CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Making a transparent jpanel

    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.

    I would appreciate any suggestions.
    Maureen
    Attached Files Attached Files

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Making a transparent jpanel

    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.

  3. #3
    Join Date
    Apr 2009
    Posts
    4

    Re: Making a transparent jpanel

    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!)


    Maureen

  4. #4
    Join Date
    Apr 2009
    Posts
    4

    Re: Making a transparent jpanel

    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.


    Thanks for the feedback!

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Making a transparent jpanel

    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.
    Don't you mean you made them all transparent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured