Results 1 to 12 of 12
  1. #1
    onefootswill is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default [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:

    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);
         }
    ...
    }
    If anyone can see what I am missing, I would love to hear from them.
    Cheers

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    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.

  3. #3
    Rooneyz is offline Member
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    25
    Rep Power
    0

    Default

    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.

  4. #4
    onefootswill is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    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.

  6. #6
    onefootswill is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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);        
        }

  8. #8
    onefootswill is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    Cool. Thanks.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #11
    onefootswill is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you must use paint, does your paint method override call "super.paint(g) as its first method?

Similar Threads

  1. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 07:03 AM
  2. JLabel .setActionCommand
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 04:16 AM
  3. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-07-2007, 11:54 PM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 01:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •