简体   繁体   English

在运行时创建JComboBoxes

[英]Creating JComboBoxes at runtime

I have a JComboBox with numbers like: 我有一个JComboBox,其编号如下:

JComboBox test;
String[] a = new String[5];
    for (int i = 0; i < 5; i++) {
        a[i]=i+1 + "";
    }
test = new JComboBox(a);

Now I want to spawn the number selected of JComboBoxes: 现在,我想生成选定的JComboBoxes的数量:

test.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String player =test.getSelectedItem();
            //Insert code here to create a variable number of Combo Boxes
        }
    });

The code looks messy. 代码看起来很乱。 This is not what I am trying to do but it's a huge help for what I want. 这不是我要尝试执行的操作,但这对我想要的功能有很大帮助。

Ignore the fact that numbers are as String within the code a parseInteger would do. 忽略parseInteger将在代码中将数字作为String的事实。

Thanks in advance. 提前致谢。

public void actionPerformed(ActionEvent e) {
        int i = combo1.getSelectedIndex();
        combo2.setModel(models[i]);
    }

    @Override
    public void run() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

Code Courtesy:- Dynamic JComboBoxes 代码提供:- 动态JComboBoxes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM