I am writing a Java program which is supposed to display a "picture" of a switch and then add status boxes over each port. These boxes which I implemented as jpanels change color based on the status of the port, they also have tool tip info.
I can successfully read the jpeg file and paste it to a jlabel in my jframe. I can update my status jpanels as well.
My problem is that I can't seem to get the transparency working on the status jpanel. My switch jpeg doesn't show through. The setOpaque just isn't working. I have stripped down the code and included it. For simplicity, I only have a single status jpanel indicating the port for the router connection.
ESwitchDetailsPanel contains a JPanel (why have a JPanel who's only child is a JPanel?) which is opaque so it is drawing its background over its transparent parent.
ESwitchDetailsPanel is a panel which will ultimately contain 48 jpanels, one for every port on the switch. I only showed the one to the router in the example for simplicity.
My thinking is
jframe contains jpanel and jlable where jlabel has switch jpeg.
next create eswitchDetails jpanel of 48 port jpanels where the 48 port jpanels do lots of work related to status - tooltips, bordercolor, listener. Amazingly, this part works.
I add eswitchDetails panel to frame and set opaque = false, should I also set the 48 port panels false, too? I'll have to try it.
Is there a better way to do this?
I had also successfully painted the image directly on the jframe, but still had problems with transparency, so I switched to the jlabel approach just to try something different. I will freely admit Java GUIs are a bit over my head (understatement of the year!)
I'm not 100% sure what fixed my problem but I did get it working.
To summarize:
I deleted the first jpanel where I had the added the label containing the jpeg, instead I just added the jlable directly to the content pane of the frame.
Then I made each of the port jpanels opaque in addition to the jpanel which contained them. That got me to what I was after.
I add eswitchDetails panel to frame and set opaque = false, should I also set the 48 port panels false, too? I'll have to try it.
Is there a better way to do this?
There are no shortcuts, but to make sure the parent panel and all its children are in the same transparent/opaque state do the following:
In the ESwitchDetails class override the setOpaque() method. In the overridden method call super.setOpaque(..) to set the state of the parent panel and then call setOpaque(..) on each of the child panels.
I deleted the first jpanel where I had the added the label containing the jpeg, instead I just added the jlable directly to the content pane of the frame.
That shouldn't make any difference because the content pane of a JFrame is a JPanel.
Then I made each of the port jpanels opaque in addition to the jpanel which contained them.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.