Question

I wil select multiple values from an JOptionpane in Java. What is the solution to get these multiple values out of this pane. When I tried always got only one value back while i selected two or more selections.

String bigList[] = new String[bankReferentie.aantalKlanten()];

for (int i = 0; i < bigList.length; i++) {
    bigList[i] = bankReferentie.getKlanten(i).toString();
}

JOptionPane.showMessageDialog(null, new JList(bigList), "Rekening", JOptionPane.PLAIN_MESSAGE);
Was it helpful?

Solution

The trick here is to create a JList before showing the option pane, then query it after the option pane is shown.

import java.awt.BorderLayout;
import java.util.List;
import javax.swing.*;

class MultiSelectListInOptionPane {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout());

                String[] fruit = {"Apple", "Banana", "Grapefruit", "Orange"};
                JList<String> list = new JList<String>(fruit);
                gui.add(new JScrollPane(list));

                JOptionPane.showMessageDialog(
                        null, 
                        gui,
                        "Rekening",
                        JOptionPane.QUESTION_MESSAGE);
                List items = (List)list.getSelectedValuesList();
                for (Object item : items) {
                    System.out.println("Selected: " + item);
                }
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

E.G. Output

run:
Selected: Banana
Selected: Orange
BUILD SUCCESSFUL (total time: 7 seconds)

OTHER TIPS

From javadocs http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

Object[] getOptions() Returns the choices the user can make.

I had the following requirements:

Multi-Selection list of values 
Have a single value pre-selected
Provide an OK / Cancel option 
Prompt the user to choose

I need to know whether the user selected something, or canceled the operation - possibly because the selection items didn't contain something they were interested in. or maybe they just decided no to proceed.

It seems like the only way to do this with a JOptionPane is as follows:

    String[] names = {"name", "name2", "etc"};
    JList<String> list = new JList<String>(names);
    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.setSelectedIndex(0); // default selection

    JPanel gui = new JPanel(new FlowLayout());
    gui.add(new JScrollPane(list));

    int result = JOptionPane.showOptionDialog( frame,
                        gui,
                        "Choose Something (for shit sake !)",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.PLAIN_MESSAGE,
                        null,  // icon
                        null,  // list
                        null); // initial selection

Notice that the list and initial selection are null - since the they are provided in the JPanel object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top