0

I am working on a Java Swing application with JDK 11. I have a requirement that the dialog should always be on top, with minimal content when it is out of focus. But when the user interacts with the dialog, it should get more content.

I tried to use a WindowFocusListener, add added a setUndecorated(false) when the dialog is in focus. But that throws an exception - "The dialog is displayable."

As per the documentation, we can not modify a JDialog once it is on. Any suggestions on how I can implement this requirement?

public class DeveloperClient extends JDialog implements WindowFocusListener {

 ....

    @Override
    public void windowGainedFocus(WindowEvent e) {
        setUndecorated(false);
    }

    @Override
    public void windowLostFocus(WindowEvent e) {
        setUndecorated(true);
    }
}

This causes 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.smartdeveloperhub.client.ui.DeveloperClient.windowGainedFocus(DeveloperClient.java:74)
    at java.desktop/java.awt.Window.processWindowFocusEvent(Window.java:2125)
    at java.desktop/java.awt.Window.processEvent(Window.java:2041)
Vikas
  • 626
  • 1
  • 10
  • 22
  • You say your aim is to add more content. Then you get the exception not because of modifying content but the decoration. What happened to your content plans? – Queeg Aug 15 '22 at 17:55
  • 2
    Notice the first line of the stack trace contains `java.awt.Dialog.setUndecorated`. The first thing you should have done was look at [the documentation of Dialog.setUndecorated](https://docs.oracle.com/en/java/javase/18/docs/api/java.desktop/java/awt/Dialog.html#setUndecorated(boolean)). – VGR Aug 15 '22 at 18:39
  • Yes I read that. Hence the question. How do I implement such a requirement where I need to modify the dialog after it is displayed. – Vikas Aug 16 '22 at 10:46

0 Answers0