When is the size of the JComponent is calculated? After being shown in the screen or before that?
if I send .getSize()
message before .setVisible(true)
, would it give me the right answer?
Thanks

- 960
- 2
- 10
- 18

- 105
- 2
- 7
-
2Why are your interested in knowing? I have made many (many) GUIs, and rarely have to bother with the fine details of how big the components will be when on-screen. There are various reasons that people generally want to know this information - most of them bad & unnecessary reasons. Will your use-case surprise me? – Andrew Thompson Aug 22 '11 at 03:00
-
I wanted to create a JWindow that will expand when the user clicks on a "more options"-button, and i wanted to know in advance the parameter of the .setSize() message, and the problem is that the number jCheckBoxs to add is a run time parameter (the number of files on a particular folder) – Man o War Aug 22 '11 at 03:13
4 Answers
I sometimes check the sizes of my components when debugging to find out why I can't see them for instance. In most cases, the sizes will be realized when the GUI has been rendered. This can occur when pack()
or setVisible(true)
has called on the top-level window. My usual sequence of method calls is to call pack()
first as this tells the layout managers to lay out the components that they are responsible for, and sets the sizes of the components and the GUI, then call setLocationRelativeTo(null)
to center my GUI, then call setVisible(true)
to display it.

- 283,665
- 25
- 256
- 373
-
2`setLocationRelativeTo(null)` - I generally use `setLocationByPlatform(true)` for GUIs running in 1.5+ JREs. – Andrew Thompson Aug 22 '11 at 04:33
-
(chuckles) It's just that a GUI in the middle of the screen looks so.. "splash-screen'ish" I keep waiting for them to disappear and the **real** GUI to appear. Actually.. could you ask this as a question? I'd like to offer a more detailed answer - with screen shots. :) – Andrew Thompson Aug 22 '11 at 04:43
-
@Andrew: Done and you can find it here: [how-to-best-position-swing-guis](http://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis) – Hovercraft Full Of Eels Aug 22 '11 at 04:53
The layout manager is responsible for determining the size of a component, so you don't know its actual size until the component has been added to the frame and the frame has been pack()ed ore made visible.
If you use a layout manager that respects the preferred size of a component then you can use:
component.getPreferredSize();
Why do you think you need to know the size? Generally you don't worry about sizes and let the layout manager do its job.

- 321,443
- 19
- 166
- 288
-
I wanted to create a `JWindow` that will expand when the user clicks on a "more options"-button, and i wanted to know in advance the parameter of the `.setSize()` message – Man o War Aug 22 '11 at 02:41
-
@Man o War, Again, why do you need to know the size? All you do is add the compnent to the window and then pack() the window. Why is the size important? How does your logic change if the size is 10 or 300? – camickr Aug 22 '11 at 02:57
-
Another option with a "More" button, might be do add an invisible panel containing the more detail. Then when you click the button you make the panel visible and pack() the frame. – camickr Aug 22 '11 at 03:04
-
well, i first thought i will be using jContainer.setSize(theOldContainerSize + newComponentSize * numberOfComponentsToAdd) ^^' – Man o War Aug 22 '11 at 03:06
-
the problem is the number of checkBoxs cant be determined until run time (it's the number of files in a particular folder), that's whythe invisible panel cant be a solution; INMHO – Man o War Aug 22 '11 at 03:09
-
1@Man o War, It doesn't matter how many files are in the folder. What happens if you have 1000 files? YoOu can't possibly display all the files and related checkboxes on the window at one time. The proper design is to use a scrollpane of some predetermined size. Then you add your panel, that contains the checkboxes to the scrollpane. Scrollbars will appear as required. I see no need to know the size of the component in advance. – camickr Aug 22 '11 at 03:21
-
1yes, now thanks to you and others who bothered answer this, i can now see clearer. No need to know that in advance, i'll just make an invisible jScrollPane, and show it when user prompt to. This is way to easy, and solid – Man o War Aug 22 '11 at 03:28
In addition to the usual pack()
> setVisible(true)
> getPreferredSize()
sequence, you can validate()
the relevant Container
to preview the geometry, as shown here.
If I understand properly, the reason why you want to know the size of a component is to reset the size of a JWindow once a user click on the "More options" button, isn't it?
I would suggest to do the following: when the user clicks on that button, update your UI adding the extra component, and the execute pack()
on the JWindow
. It should resize to the proper size.

- 644
- 3
- 8
-
3+1 That's what I was thinking. But unfortunately it is not that simple in this case. If a directory contains 100s (or thousands) of files, it might produce a `JWindow` that is larger that the available screen space. For this case, I would recommend putting the check boxes into a `JScrollPane` that is placed in a `JPanel`. The `JPanel` in turn would have a maximum size set. After the check boxes fill the available space, the scroll bars appear. – Andrew Thompson Aug 22 '11 at 04:11
-
Definitely, if the component is too big, it should be inside a JScrollPane. – Luismahou Aug 22 '11 at 11:59