Re: A JLabel's Size
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 07 Mar 2007 08:45:09 -0800
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/
.
- Follow-Ups:
- Re: A JLabel's Size
- From: Jason Cavett
- Re: A JLabel's Size
- References:
- A JLabel's Size
- From: Jason Cavett
- A JLabel's Size
- Prev by Date: Re: Splash Screen in JDK 6
- Next by Date: Re: Splash Screen in JDK 6
- Previous by thread: A JLabel's Size
- Next by thread: Re: A JLabel's Size
- Index(es):
Relevant Pages
|