Re: A JLabel's Size



A. Bolmarcich wrote:
On 2007-03-07, Jason Cavett <jason.cavett@xxxxxxxxx> wrote:
On Mar 7, 11:45 am, Knute Johnson <nos...@xxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Jason Cavett wrote:
Something I'm curious about...
When I create a JLabel and place it into a JPanel/JDialog/some other
component, it always appears as the correct size (just large enough to
display the text + appropriate boundary around the text). But, when I
attempt to get that JLabel's size, it returns 0 width and 0 height
unless I specifically set the size of the JLabel.
Shouldn't the size have been set correctly upon the addition of the
text to the label? I'm confused on how this is working and why it's
working that way.
The component has to be realized first. It gets realized when the
container it is in is either packed or its size is set.

Excuse the quibble, but the size of the component is set by pack() as
a side effect of pack() validating (laying out) the Window. Validating
a Container causes the LayoutManager of the Container to set the sizes
of the Components in the Container. (I'll skip the detail of the
Window being made displayable.)

Not a quibble, you are correct. pack() or setVisible() are the correct conditions.

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: A fixed size window in Java
    ... because every time I call "pack" because some ... components of that window change, the Window is resized to fit exactly ... Bounds relative to the container. ... Lot of work, but any kind of custom layout always is, and LayeredPane is ...
    (comp.lang.java.programmer)
  • Re: Running an Access project crashes Access 2007 SP1
    ... That is not a very good example, because a form is a container ... so all of the 'child object' stuff already existed. ... window as a child, and cannot have siblings, but Access forms ... (The property window is still a subform window, ...
    (microsoft.public.access.adp.sqlserver)
  • Re: mmc - taskpad views - Active Directory
    ... > ADUC to another container. ... > has ADUC and the containers set up on it. ... I expand the inside console window. ... user account on the left, then choose reset password, then next and close. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Tkinter grid layout
    ... First of all, it is not a good idea to make your so-called window inherit from Frame: a Frame is not a window in tk, but a generic container. ... Creating a Frame without a container window will automatically initialize the tk toolkit, creating a default window which will become the default parent for widgets where you don't specify one. ... According to the well-known "explicit is better than implicit" principle, if a MainWindow instance are actually the main window for your application, you'd really better inherit from the Tkinter class for the main window, which is Tk. ...
    (comp.lang.python)
  • Re: custom component painting
    ... first time that a container should be laid out is as part of pack(). ... The right thing is to have the component bounds set during layout. ... you invoke pack(), the first layout is done by pack. ...
    (comp.lang.java.gui)