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); } }
source share