问题
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