frowit
frowit

Reputation: 45

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.

Upvotes: 0

Views: 493

Answers (1)

camickr
camickr

Reputation: 324128

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 );

Upvotes: 2

Related Questions