java JComboBox issue
<p>I was trying to have two Jcomboxes, where second Jcombox should changes its values according to the change in the first one.
I tried but could not succeed,Any help is appreciated. Thanks</p>

<p>This is what I have tried so Far:</p>

<pre><code>public class SharedDataBetweenComboBoxSample {

    static private String selectedString(ItemSelectable is) {
        Object selected[] = is.getSelectedObjects();
        return ((selected.length == 0) ? "null" : (String)selected[0]);
    }

    public static void main(String args[]) {
        final String labels[] = { "A", "B", "C" };
        final String labelsA[] = { "A", "AA", "AAA" };
        final String labelsB[] = { "B", "BB", "BBB" };
        final String labelsC[] = { "C", "CC", "CCC" };

        final JFrame frame = new JFrame("Shared Data");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JComboBox comboBox1 = new JComboBox();
        comboBox1.addItem(labels);
        comboBox1.setSelectedItem(null);

        final JComboBox comboBox2 = new JComboBox();
        // comboBox2.setEditable(true);
        panel.add(comboBox1);
        panel.add(comboBox2);
        frame.add(panel,BorderLayout.NORTH);

        ItemListener itemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                int state = itemEvent.getStateChange();
                System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
                System.out.println("Item: " + itemEvent.getItem());
                ItemSelectable is = itemEvent.getItemSelectable();
                System.out.println(", Selected: " + selectedString(is));
                if (selectedString(is) == "B") {
                    comboBox2.addItem(labelsB);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                } else if (selectedString(is) == "A") {
                    comboBox2.addItem(labelsA);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                } else if (selectedString(is) == "C") {
                    comboBox2.addItem(labelsC);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                } else {
                    comboBox2.setSelectedItem(null);
                    // frame.add(comboBox1, BorderLayout.CENTER);
                }
            }

        };
        comboBox1.addItemListener(itemListener);

        frame.setSize(300,200);
        frame.setVisible(true);
    }
}
</code></pre>
