JCheckBox List error?

65 views Asked by At

I'm coding an enigma machine in java, and when the program launches, I have a JOptionPane appear with 5 JCheckBoxes for the user to select which rotors to use, and in which order.

My problem is, they get added to the popup, but they aren't actually displayed. Instead I get a massive readout of all 5 checkboxes as if I called their toString method. I have a few JLabels on the popup that display correctly, along with the OK button at the bottom.

My list is initialized like so:

private final List<JCheckBox> rotorCheckBox = Arrays.asList(new JCheckBox(
        "Rotor 1"), new JCheckBox("Rotor 2"), new JCheckBox("Rotor 3"),
        new JCheckBox("Rotor 4"), new JCheckBox("Rotor 5"));

I'm not sure why it does this, it worked as an array before, and I've been trying to convert it so I don't have to constantly call Arrays.asList() on it. I've checked every use of it in my code, nothing is being called toString or creating errors relating to it being in a list.

How can I make it display correctly?

1

There are 1 answers

0
MadProgrammer On BEST ANSWER

You're adding the List to the JOptionPane, you should add the JCheckBox's to a JPanel and use that instead

So, instead of something like...

JOptionPane.showMessageDialog(null, rotorCheckBox);

You should use something more like...

JPanel panel = new JPanel();
for (JCheckBox cb : rotorCheckBox) {
    panel.add(cb);
}
JOptionPane.showMessageDialog(null, panel);

as an example