【问题标题】:How can I get an index from an JOptionPane menu如何从 JOptionPane 菜单中获取索引
【发布时间】:2021-04-10 14:04:53
【问题描述】:

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

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

【问题讨论】:

    标签: java swing joptionpane


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多