0

I have an issue with JPanels. I have two layers of JPanel. The first layer is JPanel with image and second layer show JComponents. It looks like this:

JFrame
 - JPanel1 (Image)
    - JPanel2 (JComponents)

When I have more than one component on JPanel2, then JPanel2 is not transparent but it has a grey background and I don't see JPanel1(image) under it. How to set JPanel2 as glass pane when his parent is JPanel1?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Tomm23
  • 61
  • 6
  • https://ctrlv.cz/9uet here is picture. Grey background of JPanel2 cover JPanel1 – Tomm23 Apr 25 '17 at 10:09
  • 1
    Don't overlap one panel with another. Simply draw image on the panel which holds components. [Here](http://stackoverflow.com/questions/17865465/how-do-i-draw-an-image-to-a-jpanel-or-jframe) is an example. – Sergiy Medvynskyy Apr 25 '17 at 10:10
  • Alternative: do not use an image for that. You can achieve the same effect with regular Swing components – Robin Apr 25 '17 at 10:31
  • 1
    @XtremeBaumer, don't use transparency for something like this. Check out [Backgrounds With Transparency](https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/) for the problems you will encounter. However for full transparency you just use `panel2.setOpaque(false)`. – camickr Apr 25 '17 at 15:01
  • @SergiyMedvynskyy This solution is not good for me beacause i dont use Override methods with paintComponent a have mine method where i drawImage and show components and this method call everywhere i need repaint. When i use only one JPanel then Image cover my components or components cover image with grey background too. – Tomm23 Apr 25 '17 at 20:11
  • @XtremeBaumer Swing doesn't support alpha based background colors, doing so well lead to additional painting artifacts and the practice should be avoided – MadProgrammer Apr 25 '17 at 22:02
  • @Tomm23 Have you tried making the panel transparent? `setOpaque`? – MadProgrammer Apr 25 '17 at 22:03
  • @camickr ahh okay. seems like i confused contentpane and jpanel, because content pane doesn't support `setOpaque`. thanks for pointing it out – XtremeBaumer Apr 26 '17 at 06:13
  • @MadProgrammer Yes, i did. i setOpaque to panel2, but now panel2 is real transparent. But now a see color of background of panel1 because aj try set background color to yellow of panel1. – Tomm23 Apr 26 '17 at 06:20
  • [picture](https://ctrlv.cz/GVww) with code:jPanel2.setBackground(Color.white); jPanel2.setOpaque(false); jPanel1.setBackground(Color.yellow); jPanel1.setOpaque(true); – Tomm23 Apr 26 '17 at 06:30
  • @Tomm23, Not sure what the point of your last comment is for. Your image doesn't display. Images should be loaded to this forum, not an external website. All Swing components (except JLabel) are opaque by default. So you only need to use panel2.setOpaque( false ). Setting the background of panel2 will do nothing because the entire panel is now transparent. If you code doesn't work then post an [mcve] demonstrating the problem. – camickr Apr 26 '17 at 14:57

0 Answers0