|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

08-22-2008, 02:05 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
[SOLVED] JLabel not showing on JPanel
I am having trouble getting a label to display on a JPanel-derived object which I am working on. Here is the code:
public class MyPanel extends JPanel
{
private JLabel westLightLabel;
public MyPanel ()
{
setSize(50,50);
westLightLabel = new JLabel();
westLightLabel.setSize(5, 50);
westLightLabel.setBackground(Color.RED);
westLightLabel.setLocation(0, 0);
westLightLabel.setOpaque(true);
this.add(westLightLabel);
}
...
}
If anyone can see what I am missing, I would love to hear from them.
Cheers
|
|

08-22-2008, 03:52 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
Where and how is the MyPanel being added to the GUI?
What layout manager is used, etc?
How have you tried debugging the code? Remove/change some of the set... method calls to see what happens.
|
|

08-22-2008, 04:39 PM
|
|
Member
|
|
Join Date: Aug 2008
Location: The Netherlands
Posts: 22
|
|
|
A thing you might want to try is the method setbounds() on your JLabel-object. The label might be just 1 pixel both in length and height so it's still invisible. Like norm said, this depends on your layout-manager.
Please let us know if this solves your problem.
|
|

08-23-2008, 03:33 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
MyPanel is being added to the GUI using NetBeans (I added it to the palette and dragged it on to a JFrame form). MyPanel displays perfectly. It is just the JLabel that I am having trouble getting to display on it.
I don't know much about Layout Managers, but I thought that I would be using the JPanels built-in FlowLayout .
I have tried commenting out some of those Set methods which I used. The SetBounds() method did not solve the problem.
I am guessing there is to something very small that I am missing here. Adding a label to a panel should be a beginners manoeuvre.
|
|

08-23-2008, 05:03 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
Where and how is the MyPanel being added to the GUI?
And how is the rest of the GUI being built?
I'm talking about source code here, not IDE dnd.
Write a small program to demo the problem and post it here.
|
|

08-23-2008, 05:20 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
I have figured out what the problem was. The MyPanel class overrided some method called Paint. All I had to do was add westLightLabel.paint(g); to that method, and the label appeared on the JPanel.
Thank you very much for taking the time to help me.
|
|

08-23-2008, 05:26 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
Here is a simple demo the use of a simple layout.
public static void main(String[] args) {
JFrame f = new JFrame("Label Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JLabel label = new JLabel("This is a JLabel");
label.setDisplayedMnemonic(KeyEvent.VK_U);
Container box = Box.createHorizontalBox();
box.add(label);
content.add(box, BorderLayout.NORTH);
content.add(new JButton("This is a JButton"), BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

08-23-2008, 05:32 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
Cool. Thanks.
|
|

08-23-2008, 07:28 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 876
|
|
Originally Posted by onefootswill
I have figured out what the problem was. The MyPanel class overrided some method called Paint. All I had to do was add westLightLabel.paint(g); to that method, and the label appeared on the JPanel.
Thank you very much for taking the time to help me.
This does not look to be a good solution and in fact appears very fragile, kind of like using one kluge to solve an other. I think that you still need to find out why the label wasn't showing.
The other concern of mine is that you shouldn't be overriding "paint" in a JPanel. Since this is a Swing object derived from JComponent you should override paintComponent. In fact changing this may help on the way to solving your problem.
|
|

08-23-2008, 07:31 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
Best thing for me, use layouts.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

08-23-2008, 08:03 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 7
|
|
|
You may will be correct. In fact, there are still problems in that the label is showing up in location (0,0), despite the fact that I am using SetLocation(45,0).
The problem is, I am working with an existing class which overrides the Paint method. If I don't call the Paint method of the label, it does not show up at all.
|
|

08-23-2008, 03:32 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 876
|
|
|
If you must use paint, does your paint method override call "super.paint(g) as its first method?
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|