• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Problem with JComboBox default behavior

 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic