Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-22-2008, 02:05 PM
Member
 
Join Date: Aug 2008
Posts: 7
onefootswill is on a distinguished road
[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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-22-2008, 03:52 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-22-2008, 04:39 PM
Member
 
Join Date: Aug 2008
Location: The Netherlands
Posts: 22
Rooneyz is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-23-2008, 03:33 AM
Member
 
Join Date: Aug 2008
Posts: 7
onefootswill is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-23-2008, 05:03 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-23-2008, 05:20 AM
Member
 
Join Date: Aug 2008
Posts: 7
onefootswill is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-23-2008, 05:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Here is a simple demo the use of a simple layout.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-23-2008, 05:32 AM
Member
 
Join Date: Aug 2008
Posts: 7
onefootswill is on a distinguished road
Cool. Thanks.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-23-2008, 07:28 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 876
Fubarable is on a distinguished road
Quote:
Originally Posted by onefootswill View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-23-2008, 07:31 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 08-23-2008, 08:03 AM
Member
 
Join Date: Aug 2008
Posts: 7
onefootswill is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 08-23-2008, 03:32 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 876
Fubarable is on a distinguished road
If you must use paint, does your paint method override call "super.paint(g) as its first method?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GUI - JLabel Azndaddy New To Java 8 05-02-2008 09:03 AM
JLabel .setActionCommand stevemcc AWT / Swing 1 03-28-2008 06:16 AM
Why isn't this showing? JToolTip Java Applets 2 07-08-2007 01:54 AM
JLabel Jack AWT / Swing 2 07-02-2007 03:55 PM
JLabel Freddie AWT / Swing 2 05-29-2007 04:19 PM


All times are GMT +3. The time now is 01:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org