Can I get the right JComponent size before it's shown?
Asked Answered
S

4

8

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

Suazo answered 22/8, 2011 at 2:27 Comment(2)
Why 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?Metastasis
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)Suazo
N
7

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.

Notepaper answered 22/8, 2011 at 2:36 Comment(3)
setLocationRelativeTo(null) - I generally use setLocationByPlatform(true) for GUIs running in 1.5+ JREs.Metastasis
(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. :)Metastasis
@Andrew: Done and you can find it here: how-to-best-position-swing-guisNotepaper
G
6

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.

Garay answered 22/8, 2011 at 2:29 Comment(7)
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() messageSuazo
@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?Garay
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.Garay
well, i first thought i will be using jContainer.setSize(theOldContainerSize + newComponentSize * numberOfComponentsToAdd) ^^'Suazo
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; INMHOSuazo
@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.Garay
yes, 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 solidSuazo
E
6

In addition to the usual pack() > setVisible(true) > getPreferredSize() sequence, you can validate() the relevant Container to preview the geometry, as shown here.

Ellynellynn answered 22/8, 2011 at 2:51 Comment(0)
A
6

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.

Allamerican answered 22/8, 2011 at 3:52 Comment(2)
+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.Metastasis
Definitely, if the component is too big, it should be inside a JScrollPane.Allamerican

© 2022 - 2024 — McMap. All rights reserved.