从弹出框中获取参数

时间:2012-01-13 10:29:04

标签: java swing user-interface parameters popup

我正在努力实现类似的目标:

public void execute(){
 RandomClass item = showPopupBox(randomClassArrayList).selectItem();
 randomClassArrayList.remove(item);//Obv I can make this one line.
}

showPopupBox将创建一个弹出框(转到图),为列表中的每个项目填充单选按钮列表,并在按下“确定”按钮时从列表中返回所选项目。在此之前,execute方法将等待弹出框从通过单选按钮选择的弹出框中返回该项目。

我真的不能发布任何更多信息,因为如果可以,我就不需要问了。我正在尝试通过弹出框填充参数。

我的问题只与让execute()方法等待按下弹出框的OK按钮有关,这会填充参数并完成execute()方法

1 个答案:

答案 0 :(得分:4)

你可以这样做(未经测试):

public static RandomClass showPopupBox(List<RandomClass> list)
{
    JRadioButton[] rdoArray = new JRadioButton[list.size()];
    ButtonGroup group = new ButtonGroup();
    JPanel rdoPanel = new JPanel();
    for(int i = 0; i < list.size(); i++)
    {
        rdoArray[i] = new JRadioButton(list.get(i).toString());
        group.add(rdoArray[i]);
        rdoPanel.add(rdoArray[i]);
    }
    rdoArray[0].setSelected(true);

    JOptionPane pane = new JOptionPane();
    int option = pane.showOptionDialog(null, rdoPanel, "The Title",
                                       JOptionPane.NO_OPTION,
                                       JOptionPane.PLAIN_MESSAGE,
                                       null, new Object[]{"Submit!"}, null);

    if(option == 0)
    {
        for(int i = 0; i < list.size(); i++)
            if(rdoArray[i].isSelected()) return list.get(i);
    }
    return null;
}

然后,您可以像这样使用它:

RandomClass item = showPopupBox(randomClassArrayList);