Java Linking ComboBoxes

Hello Experts,

I'm trying to link together multiple comboboxes.  Say for example I create a combo box that lists baseball postions {"infield", "outfield"}...How could I set it up so that when I select the infield another combobox appears that shows {"First", "second", "Short", "Third"}
String[] positionStrings = {"Infield", "Outfield"};
        final JComboBox positionBox = new JComboBox(positionStrings);
        positionBox.setEditable(false);
        positionBox.setBorder(new TitledBorder("Position"));

//then select infield and display another list

String[] infieldPositionStrings = {"First", "Second", "Short", "Third"};
        final JComboBox infieldPositionBox = new JComboBox(infieldPositionStrings);
        infieldPositionBox.setEditable(false);
        infieldPositionBox.setBorder(new TitledBorder("Infield Position"));

Open in new window

islandguy10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gordon_vt02Commented:
Add an ItemListener to the first combo box that changes the available selections in the second box when the value of the first box is modified.
final String[] POSITION_CATEGORIES = new String[] {
            "",
            "Infield",
            "Outfield",
            "Pitcher"
        };

        final String[][] POSITION_SELECTIONS = new String[][] {
            { "Select a Category" },
            { "First", "Second", "Short", "Third", "Catcher" },
            { "Left", "Center", "Right" },
            { "Starter", "Reliever", "Closer" }
        };
        
        final JComboBox categoryBox = new JComboBox(POSITION_CATEGORIES);
        final JComboBox positionBox = new JComboBox(POSITION_SELECTIONS[0]);
        positionBox.setEnabled(false);

        categoryBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                int index = categoryBox.getSelectedIndex();
                positionBox.setModel(new DefaultComboBoxModel(POSITION_SELECTIONS[index]));
                positionBox.setEnabled(index > 0);
            }
        });

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
islandguy10Author Commented:
Perfect!
0
islandguy10Author Commented:
Worked great, thanks a bunch!
0
gordon_vt02Commented:
no problem.  glad it helped!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.