如何在Swing中更新JComboBox实例? [英] How to update JComboBox instances in Swing?

查看:72
本文介绍了如何在Swing中更新JComboBox实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个组合框,选择第一个组合框后,其余的应该更新,但是我的代码似乎无法正常工作.请帮助这些家伙.这是我的代码(因为我的代码很长,所以我只写错误部分).

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

提前谢谢....

推荐答案

如果您打算在用户进行选择时更改组合框的值,那么最好使用ActionListener.

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

如果您希望用户每次在下拉列表中选择不同的项目时都对组合框进行更新(是的,这是一个不同的事件),那么您应该使用ItemListener

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

这篇关于如何在Swing中更新JComboBox实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆