微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 JOptionPane 菜单中获取索引

如何解决如何从 JOptionPane 菜单中获取索引

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

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,我可以调用 store 类并从中获取我想要的任何内容

解决方法

我正在尝试获取用户选择的索引

showInputDialog(...) 只返回被选中的字符串。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。