21

Let's say I want to make the opacity of a JPanel %20 viewable? I don't mean setOpaque (draw or not draw) or setVisible (show or hide)... I mean make it see-through JPanel.. you know?

Is this possible?

test
  • 17,706
  • 64
  • 171
  • 244
  • His current question is worded better, but he has asked a very similar question in the past in which he even accepted an answer (for reference): http://stackoverflow.com/questions/3517722/java-transparent-jscrollpane – Serplat Aug 25 '10 at 01:33
  • Yeah that didn't work for me. :( – test Aug 25 '10 at 02:08
  • 2
    I would help if you explain what didn't work. – trashgod Aug 25 '10 at 03:10
  • 1
    See also [*How to Create Translucent and Shaped Windows*](http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html). – trashgod Mar 16 '13 at 09:05

6 Answers6

24

You need to set the "alpha" value for your Color object:

panel.setBackground( new Color(r, g, b, a) );

However, this will still not work properly as Swing doesn't paint transparent background properly so you need to do custom painting. The basic logic is:

JPanel panel = new JPanel()
{
    protected void paintComponent(Graphics g)
    {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
};
panel.setOpaque(false); // background of parent will be painted first
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);

Check out Backgrounds With Transparency for more information on the actual painting problem and why the above code solves the problem. The link also contains a custom reusable class that does the above painting for you.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 6
    the "a" is the alpha setting which is an opacity. – Paul Jowett Aug 25 '10 at 03:07
  • Correct, you can also read the API for the Color class to get a complete description of the parameters for the method. – camickr Aug 25 '10 at 04:10
  • Thank you. I want to say I am sorry. I should have been more clear. – test Aug 25 '10 at 19:10
  • Ok, but how to do that with a JPanel conataining a background image (to see through the image) ? @janetsmith answer with AWTUtilities does not work for JPanels... – Benj Oct 20 '13 at 21:22
15

Use the alpha attribute for the color.

For instance:

panel.setBackground(new Color(0,0,0,64));

Will create a black color, with 64 of alpha ( transparency )

Resulting in this:

sample

Here's the code

package test;

import javax.swing.*;
import java.awt.Color;
import java.awt.BorderLayout;

public class See {
    public static void main( String [] args ){
        JFrame frame = new JFrame();
        frame.setBackground( Color.orange );


        frame.add( new JPanel(){{
                        add( new JLabel("Center"));
                        setBackground(new Color(0,0,0,64));
                    }} , BorderLayout.CENTER );
        frame.add( new JLabel("North"), BorderLayout.NORTH);
        frame.add( new JLabel("South"), BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible( true );
    }
}

With out it it looks like this:

setBackground( new Color( 0,0,0 )  ); // or setBackground( Color.black );

alt text

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
3
AWTUtilities.setWindowOpacity(aWindow, aFloat);

Where aWindow is the Swing component, and aFloat is the opacity.

Mike
  • 19,267
  • 11
  • 56
  • 72
1

If you have a custom panel and want the whole thing to be semi-transparent, I advise you to do override its method paintComponent like this :

@Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    }
Jack'
  • 1,722
  • 1
  • 19
  • 27
1

It doesn't work so well on windows 7.

panel.setBackground( new Color(r, g, b, a) );

the alpha channel just lightens the color.

when an element is updated on a color with an alpha channel, the computer gets confused and resets the background of the updated element without an alpha. I'm going to try

AWTUtilities.setWindowOpacity(aWindow, aFloat);

next.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

How about override the paintComponent method of the JPanel(in order to do this you have to sub class the JPanel itself and implement your own paintComponent method) inside the paintComponent you can retrieve a buffered image of the component, from there you can manipulate the alpha of the buffered image and paint it back to the JPanel. I have red this long time ago. Still looking for the code.

djakapm
  • 175
  • 7