2016-04-10 40 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

相關問題