0

How to make my JFrame auto-resizable? Size depends on existing components in the frame, but the user can add more components dynamically.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ariel Grabijas
  • 1,472
  • 5
  • 25
  • 45

2 Answers2

6

You could use the JFrame.pack() method, that according documentation:

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
  • 1
    It's the best way to do this as far as I know. Although it is not at all "auto"... You would have to call this method everytime, something changes (you add/remove/resize a Component)... +1 – brimborium Jun 11 '12 at 15:39
  • this is right, and in fact it would be a bit weird if the window resizes every single time you add a component... so you just need to invoke pack() ;) – Francisco Spaeth Jun 11 '12 at 15:43
1

See the Nested Layout Example for both dynamically added components (labels added using a button - in a scroll pane), as well as a resizable GUI.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433