簡體   English   中英

如何從 JOptionPane 菜單中獲取索引

[英]How can I get an index from an JOptionPane menu

我正在嘗試獲取用戶選擇的索引,以便我可以使用下拉菜單中的數字調用 class object。

String[] storeListArr;
Object storePick;
ArrayList<String> storeList = new ArrayList<>();

while (!(newStore[nCount] == null)) {
    storeList.add(newStore[nCount].getName());
    nCount++;
} //end loop

storeListArr = new String[storeList.size()];
storePick = JOptionPane.showInputDialog(null, "Choose Store", "Store Selection", JOptionPane.QUESTION_MESSAGE, null, storeListArr, storeListArr[0]);

因此,如果用戶從下拉菜單中選擇 newStore1,我可以調用商店 class 並從中獲取我想要的任何內容。

我正在嘗試獲取用戶選擇的索引

showInputDialog(...)只返回被選中的字符串。

如果您想知道索引,那么您需要使用ArrayList的方法:

storePick = JOptionPane.showInputDialog(...);
int index = storeList.indexOf( storePick );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM