views:

56

answers:

2

Hello, 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.

A: 

I believe most platforms will honour setMaximumSize and setMinimumSize. There is also setMaximizedBounds.

What doesn't work is adding a listener to reset the width. Threading issues makes it look unpleasant. If you have PL&F decorated windows (not supported by the Windows PL&F), then they can be hacked about with.

Tom Hawtin - tackline
+1  A: 

I do not think there is a method expressly for that purpose. However, you could preset the JFrame's preferred, minimum, and maximum size such that the widths are all equal.

Dimension dimPreferred = frame.getPreferedSize();
Dimension dimMinimum = frame.getMinimumSize();
Dimension dimMaximum = frame.getMaximumSize();
dimPreferred.setWidth( FIXED_WIDTH );
dimMinimum.setWidth( FIXED_WIDTH );
dimMaximum.setWidth( FIXED_WIDTH );
frame.setPreferredSize( dimPreferred );
frame.setMinimumSize( dimMinimum );
frame.setMaximumSize( dimMaximum );

You will probably want to do this after frame.pack() and before frame.setVisible(true).

Noel Ang