JOptionPane with a few buttons on each line?

How do I display JOptionPane.showinputDialog() with multiple JButtons in each row? I'm not talking about the Yes , No , Cancel , but a few custom flagged JButtons that appear in the content area of JOptionPane.showinputDialog ?

so I will need to get the value of the button pressed from JOptionPane .

+4
source share
3 answers

You can put any JComponents in a JOptionPane , I don’t see any restrictions there, JOptionPane is the same top-level container as JFrame , JDialog or JWindow , but unlike simple Top-Level Containers , JOptionPane implemented return events from the built-in functionality in the value Integer , meaning buttons YES , NO , OK , CANCEL and CLOSE too,

put all the jbuttons in an 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); 
+6
source

It is so close that you want, because 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
source

Do you need buttons on separate lines? Here's how to do it using JOptionPane . In this example, the text of the pressed button is used as the input value of the panel. This is done by the action created in createChoiceAction() . The multi-line keypad becomes the "message" provided by 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(); } } 
+1
source

Source: https://habr.com/ru/post/1388331/


All Articles