Search code examples
javaswingjbuttonjoptionpane

Create an option pane without buttons


In Swing how can I create an option dialog box that doesn't have any buttons or the icon at the top?

I want to use JOptionPane.showOptionDialog, but there are always buttons at the bottom.


Solution

  • To remove the icon, set the message type to JOptionPane.PLAIN_MESSAGE.

    To create a dialog box without buttons you do 2 things.

    1. Set the option type to JOptionPane.DEFAULT_OPTION
    2. Set the options array to an empty array: new Object[] {}

    The final code looks like this:

    JPanel panel = new JPanel();  // Create and modify this panel
    JOptionPane.showOptionDialog(null, 
                                 panel, 
                                 "Dialog Title", 
                                 JOptionPane.DEFAULT_OPTION,
                                 JOptionPane.PLAIN_MESSAGE, // NO Icon
                                 null, 
                                 new Object[] {},  // No options
                                 null);