简体   繁体   中英

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.

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.

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

The showInputDialog(...) only returns the String that was selected.

If you want to know the index then you need to use methods of the ArrayList :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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