How to set enable/disable so many JCombobox Dynamically by user input?
I have simple design with multiple JComboBox
like this:
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
?
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? I ask the same to you -- why are you making things unnecessarily harder for yourself?
If you created an ArrayList of JComboBoxes--
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:
// 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.
But other issues abound: JTextFields should almost never have KeyListeners added to them as this can corrupt their basic functioning. 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? Then use a 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?
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.
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());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.