简体   繁体   English

如何使JDialog与复选框

[英]how to make JDialog with checkboxes

I am writing a Java program and I ran into a problem. 我正在编写Java程序,但遇到了问题。 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. 我有一个ArrayList<JCheckBox> ,我想显示一些带有这些复选框的对话框窗口,因此我可以选择其中的一些,并希望在关闭该对话框后将选定对象的另一个ArrayList<>作为结果。 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.. 我想可以通过将ActionListener添加到这些复选框来获得结果,但是我不知道如何将ArrayList<JCheckBox>传递到对话框窗口。

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: showConfirmDialog方法必须解释消息对象才能正确呈现它,并且它不知道如何解释ArrayList ,您必须将所有元素添加到JPanel例如:

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: Object[]例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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