Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JLabel with text has dimension of 0

808423Nov 24 2010 — edited Nov 25 2010
if the following is run the system.out message says the size of the label is 0,0 why is this? in my real program what i am wanting to do is have an array of words each separately on JLabels and using a slider I am removing the text of random labels but i want the labels to remain the same size as they were with the text so that spaces will appear. I am using setPrefferedSize just after the label is added to the panel and using the current size (which i believe will do what i want) but the size happens to be 0 for which i cannot figure out why.

thanks
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Frame extends JFrame{
	
	public Frame(){

	JPanel panel = new JPanel();
	this.add(panel);
	
	JLabel label = new JLabel("some text");
	panel.add(label);
	System.out.println(label.getSize().toString());
	}
	
	public static void main (String[] args){
		
		Frame frame = new Frame();
		frame.setVisible (true);
		frame.setSize (350,200);
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
	}
}
This post has been answered by camickr on Nov 24 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 23 2010
Added on Nov 24 2010
11 comments
473 views