Search code examples
javaswingjdialogjoptionpane

showOptionDialog buttons on separate lines


can't get the buttons in the OptionDialog to appear on a new line. They all appear in one row, but I'd like to have them on separate lines.

I also tried setting up a frame to add to the OptionDialog (to set the max width), but it didn't work out for me either.

Any ideas/help/suggestions appreciated.

Object[] options = { "Button1", "Button2", "Button3", "Button4", 
     "Button5 On a newLine\n\n", "Button 6", "Button 7" };
int x = JOptionPane.showOptionDialog(null, "Choose a button..", "Title",
     JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
     null, options, options[0]);

Solution

  • Consider this alternative.

    combo box options

    import javax.swing.*;
    
    class Options {
    
        public static void main(String[] args) throws Exception {
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    Object[] options = {
                        "Option 1",
                        "Option 2",
                        "Option 3",
                        "Option 4",
                        "Option 5",
                        "Option 6",
                        "Option 7",
                        "None of the above" 
                    };
                    JComboBox optionList = new JComboBox(options);
                    optionList.setSelectedIndex(7);
                    JOptionPane.showMessageDialog(null, optionList, "Title",
                         JOptionPane.QUESTION_MESSAGE);
                }
            });
        }
    }