0

Hi I'm trying to transfer one object from one combobox to another and vice versa. To accomplish this, I use actionListeners or ItemListeners, to no luck that they dont answer my problems, or maybe there's just something wrong in my implementation.

Assuming we have to comboboxes, combobox1 and combobox2. Basically, 1. I first add the selected item to another combobox (combobox2) 2. I remove the selected item on the first combobox (combobox1)

When trying to debug this, i found out that everytime i am on the step of removing items, the listener of the other combobox fires, which does the same steps as above. This results into a loop, that just deletes the item, and places it back to the original combobox.

When using the ItemListener, with the proper if conditions of being selected or not, it throws a bigger error. Guys please help me..

*on edit mode/currently making an SSCE

Fasev Moweasck
  • 101
  • 2
  • 2
  • 14
  • 3
    all changes to JComboBox should be done in its model, search here for DefaultComboBoxModel, better for MutableComboBoxModel – mKorbel Aug 22 '14 at 11:34
  • question in this form isn't answerable here, for better help sooner post an SSCCE/MCVE, short, runnable, compilable with hardcoded value for JComboBox in local variable, maybe to use GridLayout instead of MigLayout for code in SSCCE/MCVE form here – mKorbel Aug 22 '14 at 11:39
  • 1
    Not an answer but a piece of advice: why don't use two `JList`s instead of combo boxes? You can easy transfer items between lists' models. – dic19 Aug 22 '14 at 12:49
  • but cant we use jcombobox instead of list? im using combobox to save space. – Fasev Moweasck Aug 22 '14 at 14:11
  • 1
    To save space, the suggestion of @dic19 can be done in a modal dialog. – trashgod Aug 22 '14 at 14:38

2 Answers2

1

Found this, as suggested by sir mKorbel. It did the trick, setting the model via setModel(DefaultComboBoxModel model) method doesnt trigger the ActionListener when it tries to add the contents of the model passed, versus the addItem(Object obj) method that fires the ActionListener causing the havoc that i described on my question above.

Thanks guys, and i learned about a new thing called DefaultComboBoxModels!

Community
  • 1
  • 1
Fasev Moweasck
  • 101
  • 2
  • 2
  • 14
1
jComboBox12.removeAllItems();
for (int t = 0; t < jComboBox11.getItemCount(); t++) 
{
 jComboBox12.addItem(jComboBox11.getItemAt(t));
}