• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox Question

 
Mark O'Neal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Been looking for an example of a JComboBox that will let you sort to a value as you type in the text.

String[] text = new String[5];

text[0] = "Able";
text[1] = "Betty";
text[2] = "Sally";
text[3] = "Sharon";
text[4] = "Wentworth";

JComboBox myBox = new JComboBox(text);
myBox.setEditable(true);

As an example, what I would like it to do is; type in an s and it moves to "Sally", then when I type in an h... it moves to "Sharon".

Prodon my newbie question.... but any help. Many thanks.

Mark....
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to code completely new custom ComboBoxModel to add, set and insert new values to model when ComboBoxEditor fires an ActionEvent (which your ActionListener handles) then Collections.sort(List) handles the sorting then you set the selected item which has been edited or inserted.
 
Mark O'Neal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.... for those interested in "one" way of doing this.... here is my first cut at it.
I think a DocumentListener may work also.......

Just a note: Still trying to get it so a "return", will pick up the currently highlighted item
in the drop down.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!