Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

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

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...