Search code examples
javaswinguser-interfacejframeresizable

JFrame resizable height ONLY


JFrame.setResizable(true) lets the user resize both the width and height of a window. Does a method exist which allows the user to ONLY resize the height?

Thanks.

Edit: The solutions below do NOT seem to work. On a 360x600 JFrame,

setResizable(true);
pack();
setMaximizedBounds(new java.awt.Rectangle(0, 0, 360, 1200));
setMaximumSize(new java.awt.Dimension(360, 1200));
setMinimumSize(new java.awt.Dimension(360, 600));
setPreferredSize(new java.awt.Dimension(360, 600));
setVisible(true);

Still allows fully stretching the width of the JFrame, and setting setResizable(false) allows nothing to be stretched.


Solution

  • The code below does the job right.

    addComponentListener(new ComponentAdapter() {
    
        @Override
        public void componentResized(ComponentEvent e) {
            setSize(new Dimension(preferredWidth, getHeight()));
            super.componentResized(e);
        }
    
    });