Transparent JPanel

Pawan Gupta picture Pawan Gupta · Apr 8, 2012 · Viewed 30.3k times · Source

I want to create a semi-transparent JPanel. I've done it by simply using RGBA value of color constructor but problem is when i m using event handling is not woking properly. My requirement is a semi transparent Jpanel when mouse enters it border of this panel became visible and if mouse exit the border shoud not visible. I have done this by following code but problem is its not working properly for transparent backgroud (RGBA) but it working fine for RGB color.

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class MDCW extends JFrame {

      private JPanel contentPane;

     /**
     * Launch the application.
     */
     public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MDCW frame = new MDCW();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MDCW() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1013, 551);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(0, 139, 139));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JPanel panel = new JPanel();

        panel.setBackground(new Color(0, 0, 0,50));
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                panel.setBorder(new LineBorder(new Color(255, 255, 255), 5));   
            }
            @Override
            public void mouseExited(MouseEvent e) {
                panel.setBorder(null);  
            }
        });
        panel.setBounds(360, 155, 215, 215);
        contentPane.add(panel);

        final JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(0, 0, 0));
        panel_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                panel_1.setBorder(new LineBorder(new Color(255, 255, 255), 5)); 
            }
            @Override
            public void mouseExited(MouseEvent e) {
                panel_1.setBorder(null);    
            }
        });
        panel_1.setBounds(84, 155, 215, 215);
        contentPane.add(panel_1);
    }
}

Answer

Kevin Reid picture Kevin Reid · Apr 8, 2012

JPanel does not support semi-transparent backgrounds. There are two steps needed to take care of this problem:

  • First, to have any correctly-functioning transparency at all, you must setOpaque(false) on the panel; otherwise you will have glitches, because an opaque panel is assumed to completely cover what is underneath its bounds.

  • However, when opaque is false, the panel also does not draw its background at all (!) so you will have to draw a background in paintComponent.

Here is a drop-in replacement class which will take care of both of these steps.

private class TransparentPanel extends JPanel {
    {
        setOpaque(false);
    }
    public void paintComponent(Graphics g) {
        g.setColor(getBackground());
        Rectangle r = g.getClipBounds();
        g.fillRect(r.x, r.y, r.width, r.height);
        super.paintComponent(g);
    }
}

I’ve checked that it works in your program if I change the first panel creation to:

final JPanel panel = new TransparentPanel();