Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1137 > unrolled thread

A JLabel's Size

Started by"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
First post2011-04-27 15:31 +0000
Last post2011-04-27 15:31 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  A JLabel's Size "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
    Re: A JLabel's Size "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
      Re: A JLabel's Size "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
        Re: A JLabel's Size "A. Bolmarcich" <a..bolmarcich@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000
          Re: A JLabel's Size "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:31 +0000

#1137 — A JLabel's Size

From"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
Date2011-04-27 15:31 +0000
SubjectA JLabel's Size
Message-ID<1173283154.860512.281850@n33g2000cwc.googlegroups.com>
  To: comp.lang.java.gui
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.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1138

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:31 +0000
Message-ID<q_BHh.14352$Ov3.1034@newsfe14.lga>
In reply to#1137
  To: comp.lang.java.gui
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.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test8 {
     public static void main(String[] args) {
         Runnable r = new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 JLabel l = new JLabel("Hello World!");
                 f.add(l,BorderLayout.CENTER);
                 System.out.println(l.getWidth());

                 f.pack();
                 System.out.println(l.getWidth());
                 f.setVisible(true);
             }
         };
         EventQueue.invokeLater(r);
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1141

From"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
Date2011-04-27 15:31 +0000
Message-ID<1173291080.282224.77460@s48g2000cws.googlegroups.com>
In reply to#1138
  To: comp.lang.java.gui
On Mar 7, 11:45 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
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.
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
>
> public class test8 {
>      public static void main(String[] args) {
>          Runnable r = new Runnable() {
>              public void run() {
>                  JFrame f = new JFrame();
>                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>
>                  JLabel l = new JLabel("Hello World!");
>                  f.add(l,BorderLayout.CENTER);
>                  System.out.println(l.getWidth());
>
>                  f.pack();
>                  System.out.println(l.getWidth());
>                  f.setVisible(true);
>              }
>          };
>          EventQueue.invokeLater(r);
>      }
>
> }
>
> --
>
> Knute Johnson
> email s/nospam/knute/

Okay, that makes sense.

But...why is "preferred size" recognized.  For example, I can do
something like this...

JLabel label = new JLabel("Hello World");
label.setSize(label.getPreferredSize());

I never set the preferred size on my own.  Is something being done
underneath the covers with preferred size?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1144

From"A. Bolmarcich" <a..bolmarcich@THRWHITE.remove-dii-this>
Date2011-04-27 15:31 +0000
Message-ID<slrneuu47i.1bra.aggedor@earl-grey.cloud9.net>
In reply to#1141
  To: comp.lang.java.gui
On 2007-03-07, Jason Cavett <jason.cavett@gmail.com> wrote:
> On Mar 7, 11:45 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> 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.)

In the example program if the invocation of pack() is replaced by
setSize(100,200), then the width of the label remains zero until after
setVisible(true) is invoked on the JFrame.  Making the JFrame visible
has the side effect of validating it.

>>
>> import java.awt.*;
>> import java.awt.event.*;
>> import javax.swing.*;
>>
>> public class test8 {
>>      public static void main(String[] args) {
>>          Runnable r = new Runnable() {
>>              public void run() {
>>                  JFrame f = new JFrame();
>>                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>>
>>                  JLabel l = new JLabel("Hello World!");
>>                  f.add(l,BorderLayout.CENTER);
>>                  System.out.println(l.getWidth());
>>
>>                  f.pack();
>>                  System.out.println(l.getWidth());
>>                  f.setVisible(true);
>>              }
>>          };
>>          EventQueue.invokeLater(r);
>>      }
>>
>> }
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/
>
> Okay, that makes sense.
>
> But...why is "preferred size" recognized.  For example, I can do
> something like this...
>
> JLabel label = new JLabel("Hello World");
> label.setSize(label.getPreferredSize());
>
> I never set the preferred size on my own.  Is something being done
> underneath the covers with preferred size?

With Java Swing, there is a lot being done underneath the covers.  For
a text label, the getPreferredSize() method determines what the size
Component needs to be to draw 1) the text of the label in the font being
used for the text and 2) the border.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1145

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:31 +0000
Message-ID<I_HHh.21738$Ov3.4477@newsfe14.lga>
In reply to#1144
  To: comp.lang.java.gui
A. Bolmarcich wrote:
> On 2007-03-07, Jason Cavett <jason.cavett@gmail.com> wrote:
>> On Mar 7, 11:45 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
>> 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/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web