简体   繁体   中英

How to get input from a JOptionPane option being clicked. - java

JOptionPane

How can I get input from the options being clicked on the JOptionPane , not the Okay button but the options I've provided such as Emerald , Autumn etc.

Here's my code

public static ArrayList<String> Skins = new ArrayList<String>();

public static void skinJPanel() {
    try {
        String[] skins = {"Emerald", "Autumn", "Black Steel", "Blue Steel", "Creme Coffee", "Green Magic"};
        for (int i = 0; i <= 5; i++) {
            Skins.add(skins[i]);
        }
        JPanel panel = new JPanel();
        panel.setSize(200, 200);
        JList list = new JList(Skins.toArray());
        JScrollPane scrollpane = new JScrollPane();
        scrollpane = new JScrollPane(list);
        panel.add(scrollpane);
        scrollpane.getViewport().add(list);
        String string = JOptionPane.showInputDialog(null, scrollpane, "Aftermath Skin Chooser", JOptionPane.PLAIN_MESSAGE);
        System.out.println(string);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Skins.clear();
}

To get the selected value AFTER the user has pressed [Okay], you should probably do something more like...

int result = JOptionPane.showConfirmDialog(null, scrollpane, "Aftermath Skin Chooser", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
    String value = (String) list.getSelectedValue();
    System.out.println(value);
}

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