简体   繁体   English

如何从 JOptionPane 菜单中获取索引

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

I'm trying to get the index of what the user chooses so that I can call the class object with the number from the drop-down 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]);

So if the user picks newStore1 from the drop-down menu, I can call the store class and get whatever I want from it.因此,如果用户从下拉菜单中选择 newStore1,我可以调用商店 class 并从中获取我想要的任何内容。

I'm trying to get the index of what the user chooses我正在尝试获取用户选择的索引

The showInputDialog(...) only returns the String that was selected. showInputDialog(...)只返回被选中的字符串。

If you want to know the index then you need to use methods of the ArrayList :如果您想知道索引,那么您需要使用ArrayList的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM