Obtaining index from the Joptionpane [duplicate]

梦想的初衷 提交于 2019-12-11 13:10:28

问题


I want to access the index of the option selected by the user.Eg, in the figure below,if i choose microsoft option then it should give me the index 1.Is this possible?


回答1:


Well you get "Microsoft" (well the Object showing Microsoft at least) as the return value from the show call, is that good enough?

If you need the index just find the index of that return value in the input array you provided to the dialog.

See the input section of the java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

Assuming you are using 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...
}


来源:https://stackoverflow.com/questions/10673486/obtaining-index-from-the-joptionpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!