This code was taken from my original code and modified for testing purposes.
Question: Why is it that after clicking on a JComboBox, I cannot click on any other JComboBoxes?
Purpose: After clicking on the JComboBox, the selection gets copied down to the JTextField.
I have read many other posts on StackOverflow and made those changes accordingly, yet they have not solved the problem.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Test implements ActionListener {
JComboBox[] cb;
JTextField[] text = new JTextField[3];
JFrame frame2;
public static void main(String[] args) {
Test t = new Test();
t.changeEntry();
}
private void changeEntry() {
frame2 = new JFrame();
frame2.setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(3, 3));
initialize(p);
JTextField url = new JTextField();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
addTextField(p, 0, url);
addTextField(p, 1, username);
addPassField(p, 2, password);
frame2.add(p, "Center");
frame2.setTitle("Entries");
frame2.setVisible(true);
frame2.setSize(500, 500);
frame2.setLocation(430, 100);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private void initialize(Panel p) {
String[] array1 = {"A"};
String[] array2 = {"B"};
String[] array3 = {"C"};
JComboBox aa = new JComboBox<String>(array1);
JComboBox bb = new JComboBox<String>(array2);
JComboBox cc = new JComboBox<String>(array3);
cb = new JComboBox[3];
cb[0] = aa;
cb[0].addActionListener(this);
cb[0].setActionCommand("A");
cb[1] = bb;
cb[1].addActionListener(this);
cb[1].setActionCommand("B");
cb[2] = cc;
cb[2].addActionListener(this);
cb[2].setActionCommand("C");
p.add(cb[0]);
p.add(cb[1]);
p.add(cb[2]);
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("A")) {
checkSelection(cb[0], 0);
} else if (s.equals("B")) {
checkSelection(cb[1], 1);
} else if (s.equals("C")) {
checkSelection(cb[2], 2);
}
}
private void checkSelection(JComboBox cb, int i) {
String str = (String) cb.getSelectedItem();
text[i].setText(str);
}
private void addTextField(Container c, int i, JTextField tf) {
tf.setText("Edit entry here");
tf.setEditable(true);
c.add(tf);
text[i] = tf;
}
private void addPassField(Container c, int i, JPasswordField pf) {
pf.setText("test");
pf.setEditable(true);
c.add(pf);
text[i] = pf;
}
}
My professor and I looked over the code and found out that JComboBoxes do not like overlapping with JTextFields. This is the modification to the code that makes the error go away:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Test implements ActionListener {
JComboBox[] cb;
JTextField[] text = new JTextField[3];
JFrame frame2;
public static void main(String[] args) {
Test t = new Test();
t.changeEntry();
}
private void changeEntry() {
frame2 = new JFrame();
frame2.setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(2, 3));
JTextField url = new JTextField();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
addTextField(p, 0, url);
addTextField(p, 1, username);
addPassField(p, 2, password);
initialize(p);
frame2.add(p, "Center");
frame2.setTitle("Entries");
frame2.setVisible(true);
frame2.setSize(500, 500);
frame2.setLocation(430, 100);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private void initialize(Panel p) {
String[] array1 = {"A"};
String[] array2 = {"B"};
String[] array3 = {"C"};
JComboBox aa = new JComboBox<String>(array1);
JComboBox bb = new JComboBox<String>(array2);
JComboBox cc = new JComboBox<String>(array3);
cb = new JComboBox[3];
cb[0] = aa;
cb[0].addActionListener(this);
cb[0].setActionCommand("A");
cb[1] = bb;
cb[1].addActionListener(this);
cb[1].setActionCommand("B");
cb[2] = cc;
cb[2].addActionListener(this);
cb[2].setActionCommand("C");
p.add(cb[0]);
p.add(cb[1]);
p.add(cb[2]);
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("A")) {
checkSelection(cb[0], 0);
} else if (s.equals("B")) {
checkSelection(cb[1], 1);
} else if (s.equals("C")) {
checkSelection(cb[2], 2);
}
}
private void checkSelection(JComboBox cb, int i) {
String str = (String) cb.getSelectedItem();
text[i].setText(str);
}
private void addTextField(Container c, int i, JTextField tf) {
tf.setText("Edit entry here");
tf.setEditable(true);
c.add(tf);
text[i] = tf;
}
private void addPassField(Container c, int i, JPasswordField pf) {
pf.setText("test");
pf.setEditable(true);
c.add(pf);
text[i] = pf;
}
}
So, for anyone having this problem, look at the differences between my question and the modification:
p.setLayout(new GridLayout(3, 3));
initialize(p);
JTextField url = new JTextField();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
addTextField(p, 0, url);
addTextField(p, 1, username);
addPassField(p, 2, password);
to
p.setLayout(new GridLayout(2, 3));
JTextField url = new JTextField();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
addTextField(p, 0, url);
addTextField(p, 1, username);
addPassField(p, 2, password);
initialize(p);