1

Since JPanel and Canvas are both same-level components, the solution would probably be some sort of a 'hack'. This question says that you won't be able to add the lightweight component to the heavyweight canvas (I want JPanel transparent).

If this isn't posible, then would putting a transparent Component work? Also, is it feasible to add Swing components to the Component (it just has to work, even if it's bad). And how would I go about actually putting it over the canvas (since they are both same-level components)?

Note: I would never do something like this in a real app, I just need it in this case

Community
  • 1
  • 1
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
  • Are you tied to using a `Canvas`? – akf Jul 24 '12 at 03:32
  • 1
    ***Why** are you tied to using a `Canvas`?* What are you actually trying to achieve here? – Andrew Thompson Jul 24 '12 at 04:30
  • @AndrewThompson I am provided a Canvas and I want to lay Swing components over it. I can't change the fact that I'm given a Canvas. – LanguagesNamedAfterCofee Jul 24 '12 at 05:17
  • Is the content of the panel static? What, specifically is in the panel? ..more information is better than less, when it comes time to explain the 'user feature' that you are trying to provide. Have you looked at the [`JLayeredPane`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JLayeredPane.html)? Java 7 offers the ability to mix Swing and AWT components without artifacts. Can your app. justify demanding Java 7+? – Andrew Thompson Jul 24 '12 at 05:20
  • Well that is one answer to 4 questions. 1 out of 4 ain't bad, it's a waste of my time. Good luck with it. – Andrew Thompson Jul 24 '12 at 06:49

1 Answers1

0

One possiblity is to add the JPanel to whatever container the Canvas is on, then setting the color of the JPanel to have an alpha of 0. This should add it over the old one, without blocking out the Canvas. Is this what you want?

EDIT: Thinking about it, the JPanel's default color is transparent... You should just be able to add the JPanel the Canvas's parent, and lay it over it

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31