Java table transparency

I want to change the transparency of JTable to see what's behind cells like a photograph, maybe?

+4
source share
3 answers

This can be done using setOpaque(false)on JTableand TableCellRenderer. The screenshot is created with the following code:

enter image description here

the code:

public static void main(String[] args) throws MalformedURLException, IOException{
    final BufferedImage image = ImageIO.read(new URL(
                  "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

    JFrame frame = new JFrame("Test");

    frame.add(new JTable(18, 5) {{
            setOpaque(false);
            setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {{
                setOpaque(false);
            }});
        }
        @Override
        protected void paintComponent(Graphics g) {
            g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
            super.paintComponent(g);
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
+6
source

You can set the background color for the alpha component:

Color backgound = new Color(0, 0, 0, 255) //total transparency

Publish SSCCE for more information about what you are trying to accomplish .

+3
source

I tried this answer:

Color backgound = new Color(0, 0, 0, 255) //total transparency

but it didn’t work. It worked:

Color backgound = new Color(0, 0, 0, 0) //total transparency
+1
source

All Articles