Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with JComboBox default behavior  RSS feed

 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have two entries in a combo box with the same text, and I select the second one, the combo box will override my selection and select the first one.

So if I have "Tax" at position one and "Tax" at position six, then select position six, the combo box overrides me and selects position one.

For my app this is bad. Can I change it?
 
Stratos Geroulis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers,

Actually there are two ways to select an entry of a combo box:
1. setSelectedIndex(int index)
2. setSelectedItem(Object obj)

So if you use the second one with the parameter "Tex" java gets every record of the JComboBox as an object and then calls the toString() method on them. He finds out the first time he finds "Tex" that they are equal so he selects that one.

Obviously you should use the first method knowing ofcorse the place of the second "Tex". If you don't then search it up through the combo box using a loop among with the method getItemAt(int index).

Thats all!
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using the first one. If I pass setSelectedIndex(6), and index 6 and index 3 have the same text, the combobox autoselects 3. The same thing happens if I select 6 using a user event.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show us your code? I wouldn't mind doing some testing with it, but I don't really want to write a test app up myself because I might be doing it differently than you.
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My project is too big, but I can demonstrate the problem with this...

http://www.wideopenwest.com/~poems/test/testcombo/src/testcombo/Frame1.java
http://www.wideopenwest.com/~poems/test/testcombo/src/testcombo/testcombo.java

If you run this, you will see the combo selections, ONE TWO THREE FOUR FIVE SIX TWO

If you select the TWO in the seventh position, then reopen the combo box again, you will notice the TWO in the second position is selected rather than the seventh.
 
Catalin Merfu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, this may be a Swing bug.

Behind the 2 'Tax' labels you must put 2 different objects, otherwise makes no sense to have the same entry twice. What I do in this situation I select based on the real object and use a custom renderer.
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, a bug. I agree. Thing is, when the user selects an entry, I am not programmically selecting the object, the combo box is doing it. The only way I can think of round this is to include the index number in the text.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come the same exact text appears in the combo twice? Is there not some context difference between the two? You could include this context in the combo to differentiate them (it may help the user out as well).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!