Hey guys,

I have an Jlabel class and trying to simply add it to the panel in my main class. However is nowhere to be seen! No errors are being output so currently have no idea what wrong...unless you can see whether my method is wrong...

Jlabel class:

public class egLabel extends JLabel{

	int angleOfRotation;
	ImageIcon icon;



public egLabel(int angleOfRotation, ImageIcon icon) {
	 this.angleOfRotation = angleOfRotation;
	 this.icon = icon;

	 this.setBounds(0,0,150,250);
	 this.setVisible(true);
}
. . .

Main class:

labelOne = new egLable(labelAngle, img);
pCenter.add(labelOne, null);

Cleo

You haven't set any text or ImageIcon for your Label, so there's nothing to display. You have stored an icon variable, but how will the JLabel paintComponent method know about it?

Oh I have, I just haven't included that in the code.

In the main publc class section I have:

public ImageIcon img = new ImageIcon("TheImage.gif"); 

int labelAngle=0 //initially!

Yes, and you pass that into your constructor, and store it in your variable, but all the code you inherit from JLabel knows nothing about that. When paintComponent is called the inherited implementation won't use your icon because it doesn't know about it.

Hmm, so I need the main class to know about 'icon', when adding it to the panel? What method would I use in addition to pCenter.add(labelOne, null) ?

No, who said anything about the main class?
You extended JLabel.
In your constructor you store two of your own variables.
You don't call a JLabel constructor, so Java just calls its default (no-args) constructor for you - its like calling new JLabel() - a label with no text and no icon.
When Java wants to construct and paint the window, it calls various methods for your egLabel, and Java uses the methods you inherited from JLabel.
BUT
those methods you inherited from JLabel only know about the variables defined in JLabel. They know nothing about the variables you defined in egLabel, so they don't know about your icon, so they don't paint it.

How do you think you could set the icon that the inherited JLabel methods will use?

Well, I've just used the setIcon(icon); method in the paintComponent method of the egLabel class. The image now appears. However if I want to set the bounds of labelOne again, it doesnt seem to listen from the main class.

labelOne.setBounds(0,0,X, Y); repaint();
labelOne.getParent().repaint();

Yes, setIcon(icon);, or super(icon); as the first line of your constructor, gives the JLabel code access to your icon. Excellent!
I'm sorry if I made you work too hard for that, but I do believe you learn so much more when think right through to the solution yourself, and not just copy/paste something.
For the setBounds - you don't say what layout manager you are using, but you may need to call pack() on the parent to get the layout manager to use your new bounds.

Thanks! No don't worry, that's great, it just took me a while getting my head around the 'Java speak' termonologies. It's all sorted now, I understand that the icon needed to be assigned in the JLabel class as that's where the label methods are inherited from and so it was constructing an empty label...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.