Swing windowStateChanged listener returns previous property values of components

218 Views Asked by At

I have a Swing application with a JSplitPane and I want to get the width of the JSplitPane as soon as the window is maximized or restored. But I can't seem to get the correct value, it always returns the previous width of the JSplitPane. Not the width after the window state is changed.

I know I have to validate using the evt.getNewState() for NORMAL and MAXIMIZED_BOTH but it doesn't matter here.

addWindowStateListener(new java.awt.event.WindowStateListener() {
    public void windowStateChanged(java.awt.event.WindowEvent evt) {
       System.out.println(jsp.getWidth());
    }
});

This prints out 1812 if restored and 1120 if maximized. I also tried this with a few other components and ended up with the same result. How can I overcome this issue?

1

There are 1 best solutions below

0
m4heshd On

I found my own solution for this problem by using addComponentListener() and handling the componentResized event for the JFrame. All the sub-components are updated in this event, so I was able to get the new width and perform my task. Still not perfect but close enough. See How to Write a Component Listener for more.