Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 759
Man o War Avatar asked Aug 22 '11 02:08

Man o War


People also ask

How do you use JComponent?

To use JComponent, the procedure is usually as follows: create a subclass of JComponent; override the paintComponent() method to draw whatever graphics are required; override getPreferredSize(), getMinimumSize() and getMaximumSize() to make your new component "behave" properly.

How to set a Border in Java Swing?

To put a border around a JComponent , you use its setBorder method. You can use the BorderFactory class to create most of the borders that Swing provides. If you need a reference to a border — say, because you want to use it in multiple components — you can save it in a variable of type Border .


1 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.

like image 132
Hovercraft Full Of Eels Avatar answered Sep 22 '22 14:09

Hovercraft Full Of Eels