tags:

views:

1321

answers:

3

Is there a good(and easy) way to make a JCombobox look like a JTextField? By this I mean there should not be a dropdown button, but when the user enters something it should show multible results.

Basically the same way google, youtube, facebook etc. works.

+2  A: 

Do you mean that you want it to behave like a text field that autocompletes against previously entered values? SwingX includes support for adding autocompletion to text components.

Another library you could look at is JIDE Common Layer. Their IntelliHints functionality might be what you are looking for. There is a demo you can download from here.

Mark
I already use a similar library called glazedList. The problem is that if I add autocompletion to a JTextField it only autocompletes one result. Try fx to write "stackoverflow" in google without searching. You will get multible results(dropdown menu) but you wont see a button like on a JCombobox.Hope this makes sense :)
bobjink
That makes sense. I have updated my answer with another suggestion.
Mark
+2  A: 
Markus Jevring
A: 

I have a very similar problem, I don't care about the popup arrow, but I need to control the text appearance when the component is disabled.

I want to show the current value, but disable list selection/editing. The only way to get this behavior with JComboBox is to use comboBox.setEnabled(false); which makes the text an unreadable light grey.

I created a ComoBoxUIDecorator, and intercepted some of the paint methods. This has exactly the right effect -- the arrow appears greyed out while the current value appears in black and readable (as if enabled).

public class ComboBoxUIDecorator extends ComboBoxUI {
 private ComboBoxUI m_parent;

 public ComboBoxUIDecorator(ComboBoxUI x) {
  m_parent = x;
 }
 ...
 public boolean isFocusTraversable(JComboBox c) {
  return m_parent.isFocusTraversable(c);
 }

 public void paint(Graphics g, JComponent c) {
  c.setEnabled(m_displayEnabledState);
  m_parent.paint(g, c);
  c.setEnabled(m_realEnabledState);
 }
}

You might try a similar approach; If you could find the arrow button, you could paint over the arrow after invoking m_parent.paint()

Justin