2

So, here's the problem: I need 3 or more jComboBoxes (all with same items) to be connected to one another so that when I select one item in any jComboBox, that item will disappear in all other jComboBoxes. Concrete example: Poker game is over. Now I need to save the players places. I have jComboBoxes (which represent places) populated with profiles of players and I want to make sure only one profile can be selected throughout jComboBoxes. I tried something on my own and gotten this far. As you will see, it's totally useless...

in MyItemListener:    
    if (jComboBox1 == evt.getSource())
    {   
        if (jComboBox1.getSelectedIndex() > 0)
        {
            jComboBox2.removeItem(jComboBox1.getSelectedItem());
            jComboBox3.removeItem(jComboBox1.getSelectedItem());
        }    
    }
    if (jComboBox2== evt.getSource())
    {   
        if (jComboBox2.getSelectedIndex() > 0)
        {
            jComboBox1.removeItem(jComboBox2.getSelectedItem());
            jComboBox3.removeItem(jComboBox2.getSelectedItem());
        }
    }
    if (jComboBox3== evt.getSource())
    {   
        if (jComboBox3.getSelectedIndex() > 0)
        {
            jComboBox1.removeItem(jComboBox3.getSelectedItem());
            jComboBox2.removeItem(jComboBox3.getSelectedItem());
        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zaista
  • 1,272
  • 8
  • 18

3 Answers3

1

Add an itemListener to each combobox and when you select an item from combobox 1 it will trigger the listener and you can retrieve the value selected. From there you can remove the same item from the other comboboxes.

george_h
  • 1,562
  • 2
  • 19
  • 37
1

You might get some ideas from this example that uses the selection in combo1 to determine which of several models to display in combo2.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

I would introduce a custom combobox model aware of all the comboboxes. The model item should have a reference to the combobox where it's selected (the reference could be null of course).

Then I would create a wrapper model which has parent combobox. The getSize() and getElementAt() should compare reference of source model element with parent reference of the wrapper model. If it's the same skip the element.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • _model item should have a reference to the combobox_ - no! A data/model item **must not** have a reference to the view, as we all know :-) -1 as an incentive to clarify – kleopatra Oct 19 '12 at 10:10
  • reference to combo is simpler. of couse we can create a mmm "selectionReference" string and check in a special map which selectionReference is used for each combo. All the rules (and MVC as well) were created to help. They are not static. I would not recomment to extand the classes or build architecfture using such things but to solve the special problem it's fine. – StanislavL Oct 19 '12 at 10:25
  • disagree (as you probably expected ;) Always very suboptimal to go completely against the wind. – kleopatra Oct 19 '12 at 10:31
  • I'm afraid you lost me there. I was hoping for an simpler solution, because I'm relatively new to java... Guess I'll just check for selections while submitting (saving) and don't allow it if there are invalid selections... – Zaista Oct 19 '12 at 11:38
  • @kleopatra [I'd be voting for one Model with view_to_model too, sure against to wind too](http://stackoverflow.com/a/12919251/714968) – mKorbel Oct 19 '12 at 12:17