Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setUndecorated(true) to a JDialog created from an instance of JOptionPane

Tags:

java

swing

I currently have a JDialog created by calling the createDialog() method from my instance of JOptionPane:

JOptionPane pane = new JOptionPane(myPanel, JOptionPane.PLAIN_MESSAGE,JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
dialog = pane.createDialog(null, "");

I wanted to be able to remove the title bar from the JDialog by calling setUndecorated(true) onto the JDialog, but I get an IllegalComponentStateException: The dialog is displayable exception when I attempt to run my program.

As far as I know, the dialog is not being displayed before I call dialog.show(), which leads me to believe that the dialog is indeed "displayable" upon instantiating the dialog through pane.createDialog() far beyond my understanding of the JDialog API.

I have attempted to call setVisible(false) prior to using setUndecorated(true), but to no avail.

Any help would be appreciated as to how or of it is possible at all to remove the title bar of a JDialog of this type. Removing the title bar from a normal JDialog is easy enough, as seen from numerous other answers to questions of this type, but I cannot seem to get it to work for a JDialog created through createDialog().

Relevant code:

            input= new JTextField(50);

        input.addKeyListener(new ConsoleKeyListener());


        input.addAncestorListener( new RequestFocusListener() );
        field = new JTextArea();
        field.setEditable(false);
        field.setLineWrap(true);
        JScrollPane area = new JScrollPane(field, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        field.setRows(10);
        field.setText(consoleText);
        JPanel myPanel = new JPanel();

        myPanel.setLayout(new BorderLayout(0,0));
        myPanel.add(input, BorderLayout.PAGE_END);
        myPanel.add(area, BorderLayout.PAGE_START);
        input.setFocusable(true);
        input.requestFocus();
        int result = 101;
        //int result = JOptionPane.showOptionDialog(null, myPanel,"", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
        JOptionPane pane = new JOptionPane(myPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);

        dialog = pane.createDialog(null, "");
        dialog.setVisible(false);
        dialog.setUndecorated(true);
        //dialog.undecorated = true;


        //dialog.setOpacity(0.55f);
        removeMinMaxClose(dialog);
        removeMinMaxClose(pane); 
        removeMinMaxClose(myPanel);
        dialog.getRootPane().setOpaque(false);

        //JDialog dialog = new JDialog();
        //dialog.setVisible(false);
        //dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        //myPanel.setUndecorated(true);
        //dialog.setUndecorated(true);
        //dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);  
        //dialog.setBounds( 100, 100, 300, 200 );  
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.out.println("yo");
            }
        });
        dialog.setVisible(true);
        dialog.show();
like image 589
Vincent Wu Avatar asked Jun 14 '13 03:06

Vincent Wu


1 Answers

You need to read the JavaDoc entry on Component#isDisplayable, then have a look at the source code for create dialog

"A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible."

Basically the dialog is packed as part of the createDialog method

Possible solution

A possible solution is to create your own dialog...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOptionPane11 {

    public static void main(String[] args) {
        new TestOptionPane11();
    }

    public TestOptionPane11() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JDialog dialog = new JDialog((Frame)null, "Boo");

                JOptionPane op = new JOptionPane("Look ma, no hands", JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
                op.addPropertyChangeListener(new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        String name = evt.getPropertyName();
                        if ("value".equals(name)) {

                            dialog.dispose();

                        }
                    }
                });

                dialog.setUndecorated(true);
                dialog.setLayout(new BorderLayout());
                dialog.add(op);
                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        });
    }

}
like image 72
MadProgrammer Avatar answered Nov 15 '22 04:11

MadProgrammer