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 8105411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:09:25+00:00 2026-06-06T00:09:25+00:00

I have an JPanel populated with several opaque custom components. Now I would like

  • 0

I have an JPanel populated with several opaque custom components. Now I would like to draw something on top of these components by overriding the paintComponent() method. My problem is that the painted stuff is placed behind the embedded components and, as they are opaque, is covered by them.

Is there any way to let the painting appear on top of the components?

Here’s a short example of what I’m trying to do:

public class DrawOnTop {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("Draw on top");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new MyPanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

class MyPanel extends JPanel {

    public MyPanel() {
        setLayout(new BorderLayout(3, 3));
        add(new JButton("Button 1"), BorderLayout.NORTH);
        add(new JButton("Button 2"), BorderLayout.CENTER);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawLine(0, 0, getVisibleRect().width, getVisibleRect().height);
    }
}
  • 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-06T00:09:27+00:00Added an answer on June 6, 2026 at 12:09 am

    You were thinking along the right lines.
    Only problem was that you should have Overridden the paintChildren() method like in the code below. This is because the paintComponent() method is called as first and does the background etc painting of the component itself (the MyPanel), then is called paintBorders() and lastly the paintChildren() which paints all that is inside of the component calling it.

    public class DrawOnTop {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame f = new JFrame("Draw on top");
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(new MyPanel());
                    f.pack();
                    f.setVisible(true);
                }
            });
        }
    }
    
    class MyPanel extends JPanel {
    
        public MyPanel() {
            setLayout(new BorderLayout(3, 3));
            JButton b1 = new JButton("Button 1");
            MouseListener ml = new MouseAdapter() {
    
                @Override
                public void mouseExited(MouseEvent e) {
                    super.mouseExited(e);
                    MyPanel.this.repaint();
                }
            };
            b1.addMouseListener(ml);
            JButton b2 = new JButton("Button 2");
            b2.addMouseListener(ml);
            add(b1, BorderLayout.NORTH);
            add(b2, BorderLayout.CENTER);
        }
    
        @Override
        protected void paintChildren(Graphics g) {
            super.paintChildren(g);
            g.setColor(Color.red);
            g.drawLine(0, 0, getVisibleRect().width, getVisibleRect().height);
        }
    }
    

    Important to notice, in the code sample, that I added also a MouseListener to repaint the panel when a mouse exits a button, otherwise the buttons would always stay over the line once mouse enters over one of them.

    But if you want to have a custom painting that is always on top of your components then I would suggest to use a glass pane. Examples of a glass pane use are provided here:

    1. Simple one.
    2. A more complex one.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I programmed something like paint. I have JPanel and I can draw on it.I'm
I have a JPanel, which I would like to detect the following events (1)
I have a JPanel that implements custom drawing to draw a background. Over this,
I have a JPanel that looks something like this: JPanel panel = new JPanel();
I have a JPanel that encapsulates two JPanels, one on top of the other.
I have a JPanel panel with a bill designed and populated inside it. I
I have a JPanel that has several (25) JLabel s on it, using a
I have a JPanel with a custom LayoutManager and trying to get it inside
I have A Jpanel with A GridLayout inside. Now I added another Jpanel inside
I have a JFrame, in this JFrame I have a JPanel that I draw

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.