简体   繁体   中英

how to make JDialog with checkboxes

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..

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);

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.

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