Cannot set maximum size for JFrame (under Windows)

Viewed 160

I am trying to limit the maximum size of a JFrame. However, this works only under Linux. Under Windows, the JFrame is freely resized. The code is pretty simple:

public class TestMaxSize extends javax.swing.JFrame {
    public TestMaxSize() {
        setSize(new java.awt.Dimension(400, 400));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 400, 400));
        setMaximumSize(new java.awt.Dimension(400, 400));
        setVisible(true);
    }                     
    public static void main(String args[]) {    
        new TestMaxSize();
    }                  
}
1 Answers

From the javadoc of method setMaximizedBounds

Note, the given maximized bounds are used as a hint for the native system, because the underlying platform may not support setting the location and/or size of the maximized windows. If that is the case, the provided values do not affect the appearance of the frame in the maximized state.

Although JFrame extends java.awt.Component it is not the same as JPanel, for example. You will notice that its superclass, java.awt.Window, overrides method setMinimumSize. Class Window interacts with the window manager of the host operating system. Hence limiting the maximum size of a Window needs to be supported by that window manager. On the Windows platform, it appears that this is not a simple thing to do. I could only find an old, third party utility that allows you to configure a maximum size for all windows. Refer to this Web page: Change The Maximum Window Size from nearly seven years ago.

Related