Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:59:31+00:00 2026-06-06T14:59:31+00:00

When I run my code then JLabel is looking behind the JPanel . Why

  • 0

When I run my code then JLabel is looking behind the JPanel. Why this is happening? I have to show the label on top of panel.

Code

public class ColoredRect extends JPanel{
          
      public double x, y, width, height;  
      public JLabel name;
      
      public ColoredRect(double x,double y,String label)
      {
          name = new JLabel(label);
          this.x = x;
          this.y = y;
          this.width = 100;
          this.height =40;
           
          setLocation((int)x,(int)y);
          setSize((int)width,(int)height);
          setBackground(Color.red);
          
          add(name);
       }

       public void paintComponent(Graphics g) {
            // Draw all the rects in the ArrayList.
        super.paintComponent(g);  // Fills with background color, white.
        name.setForeground(Color.BLACK);
        name.setVisible(true);
        name.setLocation((int)x+3, (int)y+3);
        name.setSize(20, 20);
        name.repaint();
       }
       
       public void setnewPosition(double x, double y)
      {
          this.x =x;
          this.y =y;
          this.setLocation((int)x,(int) y);
          repaint();
      }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:59:32+00:00Added an answer on June 6, 2026 at 2:59 pm

    You never used the setOpaque(), method to set it’s value as being OPAQUE. Here have a look at this example, see how you draw on the JPanel and add JLabel on it.

    import java.awt.*;
    import javax.swing.*;
    
    public class PanelPaintingExample
    {
        private ColouredRectangle cRect, cRect1, cRect2;    
        private Rectangle rect;
    
        public PanelPaintingExample()
        {       
            rect = new Rectangle(0, 0, 200, 30);
        }
    
        private void displayGUI()
        {   
            JFrame frame = new JFrame("Panel Painting Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            cRect = new ColouredRectangle(Color.RED, "LABEL 1"
                                                   , Color.WHITE
                                                   , rect);
            cRect1 = new ColouredRectangle(Color.BLUE, "LABEL 2"
                                                   , Color.WHITE
                                                   , rect);
            cRect2 = new ColouredRectangle(Color.MAGENTA, "LABEL 3"
                                                   , Color.WHITE
                                                   , rect);                                     
    
            frame.add(cRect);
            frame.add(cRect1);
            frame.add(cRect2);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new PanelPaintingExample().displayGUI();
                }
            });
        }
    }
    
    class ColouredRectangle extends JPanel
    {
        private Color backColour;
        private Color foreColour;
        private String text;
        private Rectangle rect;
    
        private JLabel label;
    
        public ColouredRectangle(Color b, String text
                                         , Color f, Rectangle rect)
        {
            this.backColour = b;
            this.foreColour = f;
            this.text = text;
            this.rect = rect;
    
            label = new JLabel(this.text, JLabel.CENTER);
            label.setOpaque(true);
            label.setBackground(backColour);
            label.setForeground(foreColour);
    
            add(label);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(200, 30));
        }
    
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(backColour);
            g.fillRect((int)rect.getX(), (int)rect.getY()
                                  , (int)rect.getWidth()
                                  , (int)rect.getHeight());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using PhoneGap,n using this code in xCode then run properly and database
If we make a thread STA like this: Thread.SetApartmentState(STA); then it cannot run code
i have a problem, when i m run my code then error is occured
I am looking for a way to run code X times, using the same
I am teaching myself Java and am getting this error when I run code
I have tried to run the following code, as a proof of concept, and
I have a setInterval running a piece of code 30 times a second. This
I am trying to run this code in android using eclips IDE. int maxrow=0;
I have the following code public static byte[] Compress(byte[] CompressMe) { using (MemoryStream ms
If your just want to compile and run some code then go back without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.