Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple choices from a JOptionPane

I have an arraylist with objects and a running Gui. I looking for a way to popup a little frame or box or something like that which displays the objects from the arraylist. The user now should be able to choose one or more items which are then returned.

I have already the optionpane but i can just select one object

    Object[] possibilities = lr.declarationList.toArray();
    String s = (String)JOptionPane.showInputDialog(
                        gui.getFrame(),
                        "Choose Target Nodes",
                        "Customized Dialog",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        possibilities,
                        null);

maybe a popup list would help out.

like image 357
user1140737 Avatar asked Jan 17 '12 18:01

user1140737


People also ask

What are the 4 JOptionPane dialog boxes?

The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

What does JOptionPane showInputDialog return?

JOptionPane. showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise. Therefore, you can just check to see if the resultant string is null .

What is JOptionPane showMessageDialog?

We call the static showMessageDialog() method of the JOptionPane class to create a message dialog. We provide the dialog's parent, message text, title, and message type. The message type is one of the following constants : ERROR_MESSAGE. WARNING_MESSAGE.


1 Answers

Try using JOptionPane.showMessageDialog(...) with a JList component argument whose elements are sourced from your list, for example:

JList list = new JList(new String[] {"foo", "bar", "gah"});
JOptionPane.showMessageDialog(
  null, list, "Multi-Select Example", JOptionPane.PLAIN_MESSAGE);
System.out.println(Arrays.toString(list.getSelectedIndices()));

Note that if you need more layout items in the message object itself you can pack them all into a JPanel and use that component as the message argument.

like image 60
maerics Avatar answered Oct 19 '22 05:10

maerics