How do you stack buttons vertically on JOptionPane using JDialogs?

I am trying to set three buttons vertically on a JOptionPane using createDialog, but this does not quite work with GridLayout. Also, I'm not sure how to get rid of the OK button. You are probably wondering why I am doing this, but you have been told to do so. I think I can use JFrame, but I don’t think it goes well with JOptionPane, because where I want the buttons to be folded.

It should be like this:
| Need help | | Help me | | Counting |

I need accessibility to add action listeners at some point, but this seems to get confusing before I can even get to this point.

import java.awt.Container; import java.awt.GridLayout; import javax.swing.*; public class ThreeButtons { static JDialog dialog; public static void main(String[] args) { JOptionPane optionPane = new JOptionPane(); optionPane.setMessage("Set Message"); optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); optionPane.setLayout(new GridLayout(3,1)); String[] buttonTxt = {"Need Help","Help Me","Counting"}; JButton[] buttons = new JButton[buttonTxt.length]; for (int i = 0; i < buttonTxt.length; i++) { buttons[i] = new JButton(buttonTxt[i]); optionPane.add(buttons[i]); } dialog = optionPane.createDialog(null, "Icon/Text Button"); dialog.setVisible(true); } } 
+4
source share
2 answers

If you want to add buttons, you need to add them to the panel and add the panel to the options panel as follows:

  JDialog dialog = null; JOptionPane optionPane = new JOptionPane(); optionPane.setMessage("Set Message"); optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3,1)); String[] buttonTxt = {"Need Help","Help Me","Counting"}; JButton[] buttons = new JButton[buttonTxt.length]; for (int i = 0; i < buttonTxt.length; i++) { buttons[i] = new JButton(buttonTxt[i]); panel.add(buttons[i]); } optionPane.setOptionType(JOptionPane.DEFAULT_OPTION); optionPane.add(panel); dialog = optionPane.createDialog(null, "Icon/Text Button"); dialog.setVisible(true); 

I'm not sure how you could get rid of the OK button, although besides manually viewing the contents of JOptionPane and deleting it. You can always create your own JDialog, then you will have full control, but there will be a bit more work to get nice joption panel icons :)

+10
source

We can get rid of the OK button using a small modification.

  JDialog dialog = null; JOptionPane optionPane = new JOptionPane(); optionPane.setMessage("Set Message"); optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3,1)); String[] buttonTxt = {"Need Help","Help Me","Counting"}; JButton[] buttons = new JButton[buttonTxt.length]; for (int i = 0; i < buttonTxt.length; i++) { buttons[i] = new JButton(buttonTxt[i]); panel.add(buttons[i]); } optionPane.setOptionType(JOptionPane.DEFAULT_OPTION); optionPane.add(panel,1); dialog = optionPane.createDialog(null, "Icon/Text Button"); dialog.setVisible(true); 

When you add a panel to the options panel, just specify the position (in this case, it is 1: it means in the middle). Therefore, the "Ok" button is omitted.

+1
source

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