JFrame resizable height ONLY

brian_d picture brian_d · Sep 8, 2010 · Viewed 12.1k times · Source

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.

Answer

Haozhun picture Haozhun · Jun 2, 2011

The code below does the job right.

addComponentListener(new ComponentAdapter() {

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

});