2

I've 3 comboboxes, upon selecting first combobox, the rest should be updated but my code doesn't seems to be working. Please help in this guys. Here is my code(since my code very long so I'll write error part only).

// example code
public class GuiComponents {
  JComboBox<String> comboBox1, comboBox2, comboBox3;

  public GuiComponents() {
     .........
     .........


     String[] element1 = {"item1", "item2", "item3"};
     String[] element2 = {"item1", "item2", item3};
     String[] element3 = {"item1", "item2", "item3"};

     comboBox1.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent event) {
            if(event.getStateChange() == ItemEvent.SELECTED) {
        // how do I update 2 comboboxes, upon selecting combobox1.
                    // combox2 should update as(element2) and
                    // combox3 should update as element3.
            }
        }
    });
  }  
}

Thanks in advance....

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Zakir Hussain
  • 414
  • 3
  • 13
  • *"(since my code very long so I'll write error part only)."* For better help sooner, post an [SSCCE](http://sscce.org/). *"my code doesn't seems to be working"* What did you try? That code snippet attempts nothing, it reads like "`// complete my work here, thanks`". – Andrew Thompson Oct 04 '13 at 03:29
  • Do I need to post all the code here? – Zakir Hussain Oct 04 '13 at 03:33
  • @user1874936 A runnable example that demonstrates the problem... – MadProgrammer Oct 04 '13 at 03:33
  • *"Do I need to post all the code here?"* What does the first 'S' of SSCCE stand for? Follow the link, **read** the article. It is all explained. – Andrew Thompson Oct 04 '13 at 03:36

1 Answers1

2

If you intention is to change the combo box values when the user makes a selection, then you are better off using a ActionListener.

If you want to the combo boxes to update each time the user selects a different item in the drop down list (and, yes, this is a different event), then you should use an ItemListener

But in either case, the process is the same...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ComboBoxUpdates {

    public static void main(String[] args) {
        new ComboBoxUpdates();
    }

    public ComboBoxUpdates() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JComboBox<String> cb1, cb2, cb3;

        public TestPane() {
            cb1 = new JComboBox<>(new String[]{"Click me", "Click me", "Click them"});
            cb2 = new JComboBox<>();
            cb3 = new JComboBox<>();
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(cb1, gbc);
            add(cb2, gbc);
            add(cb3, gbc);

            cb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cb2.setModel(new DefaultComboBoxModel<String>(new String[]{"item1", "item2", "item3"}));
                    cb3.setModel(new DefaultComboBoxModel<String>(new String[]{"item4", "item5", "item6"}));
                }
            });
        }    
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366