Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel Transparent does not work with setO  RSS feed

 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to make my 2nd JPanel on my 1st JPanel transperant. Anyone sees a problem with this?



Aim is to place the tablesOld on top of tables, with transparence. Thanks for your hints.
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setOpaque(true) makes the panel non-transparent. Use setOpaque(false) to make it transparent. See also http://en.wikipedia.org/wiki/Opacity_(optics)
 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried true and false, since I did not get any error message, but also no desired result. Is there may be another way I can check, why it is not working?
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setOpaque() is actually a misnomer. They should have called it setOpaqueGuaranteed() or something, but of course that's more wordy.

Swing needs to know if components guarantee they are opaque. Opaque in this context means that they paint every pixel within their bounds. If a component can guarantee that it will paint every pixel within its bounds, then Swing doesn't have to go through the trouble of painting other stuff beneath it.

If you want a component that's partially transparent, you will have to make sure isOpaque() returns false, so Swing knows it has to take special precautions. In JPanel, you can let isOpaque() return whatever value you set using setOpaque(). However, this is only half the issue. You still actually have to make parts transparent. You can only do this by providing your own paintComponent() method, and instead of calling super.paintComponent(g), you do all the painting yourself (and don't paint the parts you want to be transparent).
 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replay, but I have trouble turing this into a working code. Can you may provide some additional hint for. May I do also two things at a time, which cancel eachother out.

 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us your paintComponent() method, and describe what you want your component to look like. Posting your DashboardChart class would be helpful too.
 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think I have a customized version of paintComponent(), as far as I thought I have not specified anything special
As desired, please see here the DashboardChart section, may it gives you a hint:
public abstract class DashboardChart extends JPanel implements Observer


How it should look like: I have one Panel (tables), which carries another Panel (tablesOld) and on top of this is a JTable (olderTwoMonthTableModel). Tables has a pattern and this should be visible under tablesOld and olderTwoMonthTableModel.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read the question 3 times and I still am not sure exactly what you are trying to do. Posting fragments of code is not helpful.

Swing panels and layouts are 2-dimensional. If you want components "ot top" of one another, then you need to start using layered panes. So I would suggest you start by reading the section from the Swing tutorial on How to Use Layered Panes. Once you get this working you can work on opacity.

If at any time you have a problem then post a SSCCE (not your real application).

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!