Reputation: 29
I am writing a Java program and I ran into a problem. I have an ArrayList<JCheckBox>
and I want to show some dialog window with these checkboxes, so I can choose some of them and I want another ArrayList<>
of the selected objects as an outcome after closing that dialog. I think I can get results by adding ActionListener
to those checkboxes, but I don´t know how to pass that ArrayList<JCheckBox>
to the dialog window..
So far I tried something like this:
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
for (MyClass mc : sr.getFields().values())
{
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
JOptionPane.showConfirmDialog(null, al);
If I try to print the text in the checkbox, it is ok, but the dialog shows only a long line of some text that does not make any sense..
So, is there a way how to do that?
Thanks in advance..
Upvotes: 1
Views: 4999
Reputation: 22474
The showConfirmDialog
method has to interpret the message object in order to render it correctly and it doesn't know how to interpret an ArrayList
, you have to add all your elements to an JPanel
eg:
JPanel al = new JPanel();
for (MyClass mc : sr.getFields().values()){
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
JOptionPane.showConfirmDialog(null, al);
or an Object[]
eg:
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
for (MyClass mc : sr.getFields().values()){
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
Object[] obj = (Object[]) al.toArray(new Object[al.size()]);
JOptionPane.showConfirmDialog(ui, obj);
Upvotes: 3