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
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With