Results 1 to 12 of 12
- 08-22-2008, 12:05 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
[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:
If anyone can see what I am missing, I would love to hear from them.Java 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); } ... }
Cheers
- 08-22-2008, 01:52 PM #2
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, 02:39 PM #3
Member
- Join Date
- Aug 2008
- Location
- The Netherlands
- Posts
- 25
- Rep Power
- 0
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, 01:33 AM #4
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
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, 03:03 AM #5
And how is the rest of the GUI being built?Where and how is the MyPanel being added to the GUI?
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, 03:20 AM #6
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
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, 03:26 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Here is a simple demo the use of a simple layout.
Java 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); }
- 08-23-2008, 03:32 AM #8
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Cool. Thanks.
-
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, 05:31 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Best thing for me, use layouts.
- 08-23-2008, 06:03 AM #11
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
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.
-
Similar Threads
-
GUI - JLabel
By Azndaddy in forum New To JavaReplies: 8Last Post: 05-02-2008, 07:03 AM -
JLabel .setActionCommand
By stevemcc in forum AWT / SwingReplies: 1Last Post: 03-28-2008, 04:16 AM -
Why isn't this showing?
By JToolTip in forum Java AppletsReplies: 2Last Post: 07-07-2007, 11:54 PM -
JLabel
By Jack in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 01:55 PM -
JLabel
By Freddie in forum AWT / SwingReplies: 2Last Post: 05-29-2007, 02:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks