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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:16:18+00:00 2026-05-19T10:16:18+00:00

I know that a similar question was posted before , but there was no

  • 0

I know that a similar question was posted before, but there was no answer or example code.

I need a transparent JPanel on top of a canvas. The code posted below is not working

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {
    private static class Background extends Canvas{
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.RED);
            g.drawOval(10, 10, 20, 20);
        }
    }

    private static class Transparent extends JPanel {

        public Transparent() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.GREEN);
            g.drawOval(20, 20, 20, 20);
        }
    }

    public static void main(String[] args){
        JFrame frame = new JFrame();
        JLayeredPane layered = new JLayeredPane();
        Background b = new Background();
        Transparent t = new Transparent();

        layered.setSize(200, 200);
        b.setSize(200, 200);
        t.setSize(200, 200);

        layered.setLayout(new BorderLayout());
        layered.add(b, BorderLayout.CENTER, 1);
        layered.add(t, BorderLayout.CENTER, 0);

        frame.setLayout(new BorderLayout());
        frame.add(layered, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

Using the GlassPane property of the entire frame is the very last solution (highly discouraged)

  • 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-05-19T10:16:19+00:00Added an answer on May 19, 2026 at 10:16 am

    You probably will not be able to get this to work because you are mixing heavyweight and lightweight components together.

    In the past it used to be impossible to draw lightweight panels over heavyweight components like a Canvas. Since JDK 6 Update 12 and JDK 7 build 19 Java has corrected this and you can overlap the 2 correctly however it comes with limitations. Specifically in your case the Overlapping swing component cannot be transparent.

    A good description for this including the newer behaviour can be found on this page: Mixing Heavyweight and Lightweight Components Check the limitations section for your specific problem.

    I don’t think using the GlassPane will help as it is also lightweight.

    If you change the BackGround class to extend JPanel instead of Canvas you will get the behaviour you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I was already posted similar question but I just can't find
I know similar questions have been posted before, but I have specific requirements that
I know that there are many similar questions posted, but none of them refers
I know that there is a previously posted question that is very similar to
I know that this question is very similar to the question posted here .
I know that similar questions have been asked before, but I am very much
I know there are other questions that have similar issues, but I have read
I know that a similar question is posted here: Android Session cookies without using
I know this is very similar to a previous question that I had posted,
I know that very similar question was posted some time ago and it was

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.