Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JOptionPane with 4 options

I need to make a custom dialog with 4 options but as far as I can tell you can only have one with three options. Here is how I would make an option pane with 3 options:

        Frame refFrame = DialogUtils.getReferenceFrame();

        ///TODO:
        /// - Use DialogUtils
        int option = JOptionPane.showOptionDialog(refFrame,
            msg,
            rsc.str("918"),
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            DialogUtils.INFO_ICON,
            options,
            options[0]);

But I could not find some sort of open ended substitution for YES_NO_CANCEL_OPTION. Is there a way to make the JOptionPane allow four choices?

like image 715
Mike2012 Avatar asked Aug 10 '09 21:08

Mike2012


People also ask

What are the 4 JOptionPane dialog boxes?

Java AWT Programming Project The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

How do you modify JOptionPane?

JOptionPane jop=new JOptionPane(); jop. setLayout(new BorderLayout()); JLabel im=new JLabel("Java Technology Dive Log", new ImageIcon("images/gwhite. gif"),JLabel.

How do you add a button in JOptionPane?

showOptionDialog( panel, "Choose one of the following options for Category " + category + ". \n" + "If skip is available, you may choose it to skip this category.", "Select option", optionType, JOptionPane. INFORMATION_MESSAGE, null, options, options[0]);


1 Answers

You can use any of the JOptionPane's option constants, you just need to supply a options array of size 4:

public static void main(String[] args) {
    String[] options = new String[] {"Yes", "No", "Maybe", "Cancel"};
    int response = JOptionPane.showOptionDialog(null, "Message", "Title",
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
        null, options, options[0]);

    // Where response == 0 for Yes, 1 for No, 2 for Maybe and -1 or 3 for Escape/Cancel.
}
like image 146
Peter Avatar answered Sep 22 '22 01:09

Peter