I have a JFrame where some elements (one, for now) have to be centered manually on the contentPane when resizing the window or changing the window state. Resizing event (componentResized) works fine but the windowStateChanged event is causing problems because it doesn't seem to "update" the contentPane's new size properly - it keeps the previous value although the window's size is obviously changed. This can be seen by simply printing the result of getSize called on contentPane.
Centering is done by programmatically changing the constraint of the component (using putConstraint, SpringLayout). The issue is that getWidth used in that method returns "wrong" values which results in an uncentered component. Maybe another listener is needed here?
Additional info: Eclipse with WindowBuilder, Linux Mint 15, Java 1.7
Any kind of advice is appreciated although changing the layout manager isn't what I'm looking for here. I've asked the same question on Stack Overflow and that is what someone proposed as a solution.
I know about SSCCE guidelines but I cannot make this particular example both ready to compile and short.
For unknown reasons, I am unable to post the code example. I keep getting this - "Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words."
Extremely annoying.
EDIT: Managed to do it somehow, weird.
--- Update ---
addWindowStateListener(new WindowStateListener() { // no "@Override" was generated but it is the same with it public void windowStateChanged(WindowEvent e) { System.out.println("EVENT: " + contentPane.getSize() + ", " + getExtendedState() + ", " + e.getOldState()); // amusingly, states are actually correct - interchanging between 0 (Frame.NORMAL) and 6 (Frame.MAXIMIZED_BOTH) when I maximize and "unmaximize" tfArrayPanelCenter(); } }); public void tfArrayPanelCenter() { int padding = (contentPane.getWidth() - tfArrayPanel.getPreferredSize().width - HGAP) / 2; sl_contentPane.putConstraint(SpringLayout.WEST, tfArrayPanel, padding, SpringLayout.WEST, contentPane); }