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
  • 20,394
  • 79
  • 230
  • 422

2 Answers2

0

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

Gothmog
  • 833
  • 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
  • 31
  • 1
  • 3