How to make my JFrame auto-resizable? Size depends on existing components in the frame, but the user can add more components dynamically.
Asked
Active
Viewed 1,822 times
0
Andrew Thompson
- 166,747
- 40
- 210
- 420
Ariel Grabijas
- 1,412
- 5
- 24
- 45
-
It depends what you mean by 'resize' and in what form the user can 'add more'. – Andrew Thompson Jun 11 '12 at 16:34
-
Lets say, that user can define options of a city. When he type "crowded" in textfield and pull enter, under first textfield a new one is created, and also frame is resized. – Ariel Grabijas Jun 13 '12 at 13:24
2 Answers
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
- 166,747
- 40
- 210
- 420
Francisco Spaeth
- 22,761
- 7
- 65
- 103
-
1It'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
- 166,747
- 40
- 210
- 420