Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java swing Table transparency

Tags:

java

swing

jtable

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

like image 978
user1278440 Avatar asked Jan 18 '23 04:01

user1278440


1 Answers

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);
}
like image 164
dacwe Avatar answered Jan 30 '23 18:01

dacwe