JOptionPane with multiple buttons on each line?

徘徊边缘 提交于 2019-12-12 11:01:57

问题


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.


回答1:


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);



回答2:


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]);



回答3:


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();
    }
}


来源:https://stackoverflow.com/questions/8658576/joptionpane-with-multiple-buttons-on-each-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!