0

I have JDiolog created from JOptionPane

        var pane = new JOptionPane(e.getMessage(),JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION);
        var dialog = pane.createDialog("Error");
        dialog.setUndecorated(true);
        dialog.setBackground(new Color(0, 0,0,78));
        dialog.setVisible(true);
        return;

but this code throws exception

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The dialog is displayable.
  at java.desktop/java.awt.Dialog.setUndecorated(Dialog.java:1265)
  at com.quiz.server.LoginDialog.lambda$new$1(LoginDialog.java:56)
  blalablablabla.....

but I commented out these lines

    dialog.setUndecorated(true);
    dialog.setBackground(new Color(0, 0,0,78));

then it works

1 Answers1

2

The following code snippet will show the JOptionPane dialog without decorations:

    boolean defaultLFDecorated = JDialog.isDefaultLookAndFeelDecorated();
    try {
        JDialog.setDefaultLookAndFeelDecorated(true);
        JOptionPane pane = new JOptionPane("This is a message", JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION);
        JDialog dialog = pane.createDialog("Error");
        dialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        pane.setOpaque(false);
        ArrayList<Component> components = new ArrayList<Component>(Arrays.asList(pane.getComponents()));
        while(!components.isEmpty()) {
            Component c = components.remove(0);
            if(c instanceof JComponent) {
                ((JComponent)c).setOpaque(false);
            }

            if(c instanceof Container) {
                components.addAll(Arrays.asList(((Container)c).getComponents()));
            }
        }
        dialog.setBackground(new Color(0, 0, 0, 78));
        dialog.setVisible(true);
        dialog.dispose();
    }
    finally {
        JDialog.setDefaultLookAndFeelDecorated(defaultLFDecorated);
    }
Palamino
  • 791
  • 3
  • 10
  • I cant set background color with it but its working without decorations. Thanks a lot :) –  Jun 24 '19 at 17:03
  • @noone I have modified the code snippet to walk the Components and setOpaque(false) – Palamino Jun 24 '19 at 17:13