How to update JComboBox instances in Swing?

≯℡__Kan透↙ 提交于 2019-12-19 04:11:29

问题


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....


回答1:


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"}));
                }
            });
        }    
    }
}


来源:https://stackoverflow.com/questions/19172677/how-to-update-jcombobox-instances-in-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!