The essence of the problem lies in the fact that when the window is resized by dragging its borders, the internal components change their size without any errors, adjusting to the new window size. However, expanding to full-screen mode, or when “sticking” to the edge of the screen (Modern OS function), the scale of the components remains the same as during the last resizing in the usual way. And returning the window to its original size expands the components for full-screen mode.

Main class:

public class Window extends JFrame implements ComponentListener { private static final long serialVersionUID = 1L; WorldCamera screenArea; SidePanel sideBarArea; Window(int width, int height) { addComponentListener(this); setSize(width, height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); setResizable(true); setVisible(true); this.loadScreen(width, height); } void loadScreen(int width, int height) { screenArea = new GameWorldCamera(); sideBarArea = new SidePanel(); screenArea.setPreferredSize(new Dimension((width / 6 * 5) - 20, height - 50)); sideBarArea.setPreferredSize(new Dimension((width / 6) - 10, height - 50)); screenArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); sideBarArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); this.add(screenArea); this.add(sideBarArea); System.out.println("Now width: " + width + ", height: " + height); } void unloadScreen() { try { this.remove(ScreenArea); this.remove(sideBarArea); } catch(NullPointerException Ex) { System.out.println(Ex); } } public void componentResized(ComponentEvent arg0) { System.out.println("Now x:" + this.getWidth() + " y:" + this.getHeight()); try { screenArea.setPreferredSize(new Dimension((this.getWidth() / 6 * 5) - 20, this.getHeight() - 50)); screenArea.printSize(); sideBarArea.setPreferredSize(new Dimension((this.getWidth() / 6) - 10, this.getHeight() - 50)); sideBarArea.printSize(); } catch(NullPointerException Ex) { } } } 

Components:

 class WorldCamera extends JPanel { WorldCamera() { System.out.println("camera created"); } public void printSize() { System.out.println("Camera x:" + this.getWidth() + " y:" + this.getHeight()); } } class SidePanel extends JPanel { SidePanel() { System.out.println("side panel created"); } public void printSize() { System.out.println("Side panel x:" + this.getWidth() + " y:" + this.getHeight()); } } 
  • stackoverflow.com/questions/11148950/… The body of the message must contain at least 30 characters; you entered 0. - SimAlllll
  • Thank you It is strange that he did not find, I will be more careful next time! True, I did not quite understand about the message body. - Anthony Geraskin
  • The tip was great, but it only helped in half. Pressing the "maximize" button and dragging the window to the upper border of the screen works as it should, however, dragging it to the right or left border causes the same error. - Anthony Geraskin
  • I’m looking for an answer, until the issue was resolved using WindowListener, WindowStateListener and ComponentListener only partially, the application components still scale incorrectly when you drag the window to the right or left border of the screen. - Anthony Geraskin

1 answer 1

The question was resolved using -

 SwingUtilities.updateComponentTreeUI(Component) 

The method updates the contents of the component according to its current parameters. Here you can look at a specific example! SimAllllll thanks for the good link!

  • I hope that will help those who are faced with the solution of such issues. Thank! - Anthony Geraskin