0

is there any way to set the width of the JFrame fixed, but height of the JFrame dynamic? For an example,

width = 1200 
Height = will be based on number of elements

in other words

width = 1200 
Height = pack()

please help!

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 1
    I think you're best bet would be to restrict the width of the content pane by overriding it's `getPreferredSize` method – MadProgrammer Mar 19 '13 at 07:43
  • 1
    Do not set fixed sizes. Please look at this [question](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – Amarnath Mar 19 '13 at 07:53
  • Also consider `BorderLayout.EAST` or `BorderLayout.WEST`. – trashgod Mar 19 '13 at 10:50

2 Answers2

0

Use a constant for width and a variable for height, and then use the regular setPreferredSize method

Gothmog
  • 871
  • 1
  • 8
  • 20
  • I'm pretty sure that don't stop the frame from been resized (width wise) - which is how I read the question. Also, we're generally discouraged from using setPreferredSize – MadProgrammer Mar 19 '13 at 09:30
0

You probably don't need this, but for future people looking at this thread, you could declare a width constant, and set min & max size of the frame.

setMinimumSize(new Dimension(WIDTH, 0));
setMaximumSize(new Dimension(WIDTH, Integer.MAX_VALUE));
setResizable(false);

Subsequent calls to pack() will enforce the width, but allow the height to be dynamically calculated.

flynn06
  • 51
  • 1
  • 4