GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JcomboBox duplicates problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JcomboBox duplicates problem" Watch "JcomboBox duplicates problem" New topic
Author

JcomboBox duplicates problem

ishiro moh
Greenhorn

Joined: May 27, 2012
Posts: 6
Hello,
I have two JComboBoxs, the first one is filled from database, the second one is filled after user's choice on the first comboBox.
Once a choice in second comboBox is done, some JLabels are filled from database.

The problem is that when I select an item in the first combobox and then I change the choice thr first item is always present and the second is added with dupplicates.




Thanks and sorry for my english.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
sounds like you need to specify
itemEvent's getStateChange()
ishiro moh
Greenhorn

Joined: May 27, 2012
Posts: 6
Thanks for your reply Michael, i tried that but still the same problem, there is always duplicates in addition to the new items added.

ishiro moh
Greenhorn

Joined: May 27, 2012
Posts: 6
I tried removeAllItems on the comboBox, it worked but the second comboBox is giving only one items even if there is more.
if i remove the itemListener on the second comboBox it works perfectly the problem is then here but i just can't figure out whats wrong with it:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
all your code that looks that looks like this (see indicated lines)

change to this
ishiro moh
Greenhorn

Joined: May 27, 2012
Posts: 6
Thanks for your time Michael,
I still have the one item shown issue

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
comboBox1's listener fires twice as there's no ItemEvent.SELECTED separation,
and comboBox2's listener will fire as soon as you change the model, which changes
the selection (would be same for the first addItem of your original posted code).

probably easiest way would be an ItemListener class, instead of the anonymous class.
so instead of

it would go something like this

now, in comboBox1's listener:
 
GeeCON Prague 2014
 
subject: JcomboBox duplicates problem