2016-04-10 42 views
0

有没有办法获得所选项目的选定索引时使用JOptionPane与组合框?如何从JOptionPane中获取所选项目的int值?

在下面的代码中,我可以得到选定的值,但我需要选定的索引。

String input = (String) JOptionPane.showInputDialog(null, "Select Mechanic: ", 
"Select Mechanic", JOptionPane.QUESTION_MESSAGE, null, mechanics, mechanics[0]); 

是否可以通过JOptionPane获取选定的索引?

+0

经过快速Google搜索,我遇到了此StackOverFLow线程。我希望它能帮助你满足你的要求。 http://stackoverflow.com/questions/11489845/how-to-return-index-of-object-array-used-by-joptionpane-for-use-in-a-switch-stat – Aeryes

回答

0

这很简单。只是不要将结果强制转换为字符串:

int input = JOptionPane.showOptionDialog(null, "Select Mechanic: ", 
"Select Mechanic", JOptionPane.QUESTION_MESSAGE, null, mechanics, mechanics[0]); 
+0

我试过这个,但它不起作用返回类型是'对象' – forseth31

+0

@ forseth31啊我看到尝试showOptionDialog而不是showInputDialog – nhouser9

+0

showOptionDialog返回选定的索引,但它显示选项作为按钮而不是组合框 – forseth31

相关问题