[英]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.