5

How would I go about displaying a JOptionPane.showinputDialog() with multiple JButtons on each line? I am not talking about the Yes, No, Cancel buttons but multiple custom labeled JButtons that displays in the content area of JOptionPane.showinputDialog?

so I would need to get the value of the button pressed from the JOptionPane as well.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
KJW
  • 15,035
  • 47
  • 137
  • 243
  • You will some answers here: http://stackoverflow.com/questions/4223983/joptionpane-showinputdialog-with-custom-buttons – Abbas Dec 28 '11 at 16:52

3 Answers3

7

You can place any JComponents to the JOptionPane, there I can't see any limits, JOptionPane is same Top-Level Container as JFrame, JDialog or JWindow, but in contrast with plain Top-Level Containers, JOptionPane has implemented return events from built-in funcionalities in Integer value, meaning buttons YES, NO, OK, CANCEL and CLOSE too,

put all JButtons to the Array

String[] buttons = { "Yes", "Yes to all", "No", "Cancel".... };    
int returnValue = JOptionPane.showOptionDialog(null, "Narrative", "Narrative",
        JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[i]);
System.out.println(returnValue);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • the problem with this is that my application seems to hang when the joption displays....like it's transparent and the application freezes on this line. – KJW Jan 31 '12 at 19:45
  • there are three issues 1) that code for brinking JOptionPane is out of EDT (you can see only toolbar with close button), 2) code for brinking JOptionPane is linked/miss_linked to null instance, 3) JOptionPane was disposed then int return NPE, and now you have two choices a) edit this topic with code that demonstrating freeze, b) create a new post c) maybe there are/is another topics about – mKorbel Jan 31 '12 at 19:54
  • i fixed it by using `invokeLater()`. okay the problem i now have is that the buttons are appearing side by side not one per each new line. – KJW Jan 31 '12 at 20:09
  • @Kim Jong Woo maaaan what did you do :-) – mKorbel Jan 31 '12 at 20:14
  • i thought buttons would appear on each new line. that is not the case. – KJW Jan 31 '12 at 20:15
  • @Kim Jong Woo much luck with that – mKorbel Jan 31 '12 at 20:18
5

This is as close to wat you want as I can get you.

Object[] colours = {"Blue", "Red", "Black", "White"};

int n = JOptionPane.showOptionDialog(null,
    "Which colour do you like?",
    "Choose a colour",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,
    colours,
    colours[0]);

System.out.println("The users likes " + colours[n]);
Katana
  • 745
  • 2
  • 5
  • 7
  • same problem, app crashes on this line. the joptionpane window shows but none of the dioalog's content shows. – KJW Jan 31 '12 at 19:45
2

You wanted buttons on separate lines? Here's a way to get that using JOptionPane. In this example, the text of the button clicked is used as the pane's input value. This is done by the action created in createChoiceAction(). The multi-line button panel becomes the "message" provided in the JOptionPane constructor.

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


public class JOptionPaneExample {
    public static final String DIALOG_NAME = "what-dialog";
    public static final String PANE_NAME = "what-pane";

    private void showDialog() {
        JOptionPane pane = new JOptionPane(createInputComponent());
        pane.setName(PANE_NAME);
        JDialog dialog = pane.createDialog("What?");
        dialog.setName(DIALOG_NAME);
        dialog.setSize(400, 400);
        dialog.setVisible(true);
        System.out.println(pane.getInputValue());
        System.exit(0);
    }

    private JComponent createInputComponent() {
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JLabel("Pick a Category:"), BorderLayout.NORTH);

        Box rows = Box.createVerticalBox();
        ActionListener chooseMe = createChoiceAction();

        Map<String, List<String>> inputs =
                new LinkedHashMap<String, List<String>>();
        inputs.put("Cars:", Arrays.asList("Toyota", "Honda", "Yugo"));
        inputs.put("Phones:", Arrays.asList("iPhone", "Android", "Rotary"));
        inputs.put("Pets:", Arrays.asList("Dog", "Cat", "Sock"));

        for (String category : inputs.keySet()) {
            JPanel b = new JPanel(new FlowLayout(FlowLayout.LEADING));
            b.add(new JLabel(category));

            for (String choice : inputs.get(category)) {
                JButton button = new JButton(choice);
                button.addActionListener(chooseMe);
                b.add(button);
            }
            rows.add(Box.createVerticalStrut(10));
            rows.add(b);
        }

        rows.add(Box.createVerticalStrut(600));

        p.add(rows, BorderLayout.CENTER);
        return p;
    }

    private ActionListener createChoiceAction() {
        ActionListener chooseMe = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton choice = (JButton) e.getSource();

                // find the pane so we can set the choice.
                Container parent = choice.getParent();
                while (!PANE_NAME.equals(parent.getName())) {
                    parent = parent.getParent();
                }

                JOptionPane pane = (JOptionPane) parent;
                pane.setInputValue(choice.getText());

                // find the dialog so we can close it.
                while ((parent != null) &&
                        !DIALOG_NAME.equals(parent.getName()))
                { parent = parent.getParent(); }

                if (parent != null) {
                    parent.setVisible(false);
                }
            }
        };
        return chooseMe;
    }

    public static void main(String[] args) {
        JOptionPaneExample main = new JOptionPaneExample();
        main.showDialog();
    }
}
black panda
  • 2,842
  • 1
  • 20
  • 28