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)