简体   繁体   English

在NetBeans上动态启用/禁用多个JComboBox

[英]Enable/Disable Multiple JComboBox Dynamically On NetBeans

How to set enable/disable so many JCombobox Dynamically by user input? 如何通过用户输入动态设置启用/禁用这么多JCombobox?

I have simple design with multiple JComboBox like this: 我有多个这样的JComboBox简单设计:

class foo{
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JComboBox<String> jComboBox3;
    private javax.swing.JComboBox<String> jComboBox4;
    private javax.swing.JComboBox<String> jComboBox5;
    private javax.swing.JComboBox<String> jComboBox6;
    private javax.swing.JComboBox<String> jComboBox7;
    private javax.swing.JComboBox<String> jComboBox8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;

    foo(){
        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jComboBox2 = new javax.swing.JComboBox<>();
        jComboBox4 = new javax.swing.JComboBox<>();
        jComboBox3 = new javax.swing.JComboBox<>();
        jComboBox5 = new javax.swing.JComboBox<>();
        jComboBox6 = new javax.swing.JComboBox<>();
        jComboBox7 = new javax.swing.JComboBox<>();
        jComboBox8 = new javax.swing.JComboBox<>();
        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(8, 0));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(jComboBox1);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 5", "Item 6", "Item 7", "Item 8" }));
        jPanel1.add(jComboBox2);

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 9", "Item 10", "Item 11", "Item 12" }));
        jPanel1.add(jComboBox4);

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 13", "Item 14", "Item 15", "Item 16" }));
        jPanel1.add(jComboBox3);

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 17", "Item 18", "Item 19", "Item 20" }));
        jPanel1.add(jComboBox5);

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 21", "Item 22", "Item 23", "Item 24" }));
        jPanel1.add(jComboBox6);

        jComboBox7.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 25", "Item 26", "Item 27", "Item 28" }));
        jPanel1.add(jComboBox7);

        jComboBox8.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 29", "Item 30", "Item 31", "Item 32" }));
        jPanel1.add(jComboBox8);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.BorderLayout());
        jPanel2.add(jTextField1, java.awt.BorderLayout.CENTER);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                // whats todo
            }
        });
        jPanel2.add(jButton1, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }
}

so, How to implement it on Button listener if user type 4, combo1 to combo4 will be enabled , the other will disabled ? 那么,如果用户类型4, combo1combo4将被enabled ,而另一个将被disabled ,则如何在Button侦听器上实现它?

I have multiple combobox like this: 我有多个这样的组合框:

 JComboBox combo1 = new JComboBox(); JComboBox combo2 = new JComboBox(); JComboBox combo3 = new JComboBox(); JComboBox combo4 = new JComboBox(); JComboBox combo5 = new JComboBox(); JComboBox combo6 = new JComboBox(); JComboBox combo7 = new JComboBox(); JComboBox combo8 = new JComboBox(); 

Whenever I see variables named foo1, foo2, foo3, ... I think to myself, why on earth is the coder not using an array or ArrayList here? 每当我看到名为foo1,foo2,foo3 ...的变量时,我都在想,为什么程序员在这里不使用数组或ArrayList? I ask the same to you -- why are you making things unnecessarily harder for yourself? 我也向您提出同样的问题-为什么您会为自己增加不必要的麻烦?

If you created an ArrayList of JComboBoxes-- 如果您创建了JComboBoxes的ArrayList-

List<JComboBox<String>> combos = new ArrayList<>();
// code goes here to add 8 JComboBoxes to the list

Then if your user selected choice 4 and put it into a variable, index, you could easily disable all but the 4th JComboBox via: 然后,如果用户选择了选项4并将其放入变量index中,则可以通过以下方式轻松禁用除第4个JComboBox之外的所有内容:

// first disable all the combos
for (JComboBox<String> comboBox : combos) {
    comboBox.setEnabled(false);
}
combos.get(index - 1).setEnabled(true);  // enable the selected combo

I use index - 1 since lists and arrays are 0 based and the 4th JComboBox in the ArrayList is at index 3. 我使用index - 1因为列表和数组基于0,并且ArrayList中的第4个JComboBox位于索引3。

But other issues abound: JTextFields should almost never have KeyListeners added to them as this can corrupt their basic functioning. 但是其他问题比比皆是:JTextFields几乎永远都不应添加KeyListener,因为这会破坏其基本功能。 Consider adding a DocumentListener to its Document or a DocumentFilter or even better, using a JSlider or JSpinner to allow you to constrain the component to only allow acceptable input? 考虑使用JSlider或JSpinner将DocumentListener添加到其Document或DocumentFilter或什至更好的方法,以允许您将组件限制为仅允许可接受的输入? Then use a ChangeListener. 然后使用ChangeListener。 But of even bigger import, yours is a terrible design. 但是,更大的意义是,您的设计很糟糕。 Why not simply have one single JComboBox and swap its data model when the proper event occurs? 为什么在发生适当的事件时不仅仅拥有一个JComboBox并交换其数据模型?

If you used a JSpinner filled with ints, you could add a change listener to it that would select the appropriate data model for a single JComboBox and your code would be simpler, more idiot proof for the user, and most importantly, easier to debug and enhance. 如果使用填充有整数的JSpinner,则可以向其添加一个更改侦听器,该侦听器将为单个JComboBox选择合适的数据模型,并且代码将为用户提供更简单,更白痴的证明,并且最重要的是,它更易于调试和提高。

For example 例如

import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Foo2 extends JPanel {
    private static final int MIN_VALUE = 1;
    private static final int MAX_VALUE = 8;

    private JSpinner spinner = new JSpinner(new SpinnerNumberModel(MIN_VALUE, MIN_VALUE, MAX_VALUE, 1));
    private List<DefaultComboBoxModel<String>> models = new ArrayList<>();
    private JComboBox<String> comboBox = new JComboBox<>();

    public Foo2() {
        for (int i = MIN_VALUE; i <= MAX_VALUE; i++) {
            DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>();
            for (int j = 0; j < 5; j++) {
                String element = String.format("Selection: %d, Item: %d", i, j);
                comboModel.addElement(element);
            }
            models.add(comboModel);
        }
        comboBox.setModel(models.get(0));

        spinner.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                int selection = (int) spinner.getValue();
                comboBox.setModel(models.get(selection - 1));
            }
        });

        add(new JLabel("Selection:"));
        add(spinner);

        add(new JLabel("Combo:"));
        add(comboBox);
    }

    private static void createAndShowGui() {
        Foo2 mainPanel = new Foo2();

        JFrame frame = new JFrame("Foo2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

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

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