user1278440
user1278440

Reputation: 43

Java swing Table transparency

I want to change the transparency of the JTable to see what behind cells like a photo is it possible?

Upvotes: 4

Views: 4335

Answers (3)

palsch
palsch

Reputation: 7006

I tried this answer:

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

but it didn't work. The following worked:

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

Upvotes: 1

dacwe
dacwe

Reputation: 43504

This can be done using setOpaque(false) on the JTable and the TableCellRenderer. The screenshot is produced by the code below:

enter image description here

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);
}

Upvotes: 5

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

You can set background color to have some alpha component:

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

For more help on what you are trying to accomplish, post a SSCCE.

Upvotes: 3

Related Questions