从Joptionpane获取索引

时间:2012-05-20 12:41:48

标签: java swing joptionpane

我想访问user.Eg选择的选项的索引,如下图所示,如果我选择了microsoft选项,那么它应该给我索引1.这可能吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

嗯,你得到"Microsoft"(至少显示微软的Object)作为show call的返回值,是否足够好?

如果您需要索引,只需在您提供给对话框的输入数组中找到该返回值的索引。

请参阅java教程的输入部分: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

假设您正在使用showInputDialog(..):

Object[] possibilities = {"Broadcom...", "Microsoft"};
Object result = JOptionPane.showInputDialog( frame, "Capture Interfaces", "Input",   JOptionPane.PLAIN_MESSAGE, icon, possibilities, possibilities[0]);

if (result != null) {
    //result is the choosen object, if you need the index even so:
    int index = 0;
    for (Object o : possibilities) {
        if (result == o)
            break;
        index++
    }
    //index is now the index...
}