如何从 JOptionPane 菜单中获取索引

How can I get an index from an JOptionPane menu

我正在尝试获取用户选择的索引,以便我可以使用下拉菜单中的数字调用 class 对象。

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 并从中获取我想要的任何内容。

I'm trying to get the index of what the user chooses

showInputDialog(...) 只有 returns 被选中的字符串。

如果你想知道索引那么你需要使用ArrayList:

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