Background image in JScrollpane with JTable

房东的猫 提交于 2019-12-05 22:12:35
camickr

Check out the WatermarkDemo at the end of the article for a complete example.

You should subclass JTable and override its paint method so that it draws your background image. Here is some sample code:

final JTable table = new JTable(10, 5) {

    final ImageIcon image = new ImageIcon("myimage.png");

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        final Component c = super.prepareRenderer(renderer, row, column);
        if (c instanceof JComponent){
            ((JComponent) c).setOpaque(false);                    
        }
        return c;
    }

    @Override
    public void paint(Graphics g) {
        //draw image in centre
        final int imageWidth = image.getIconWidth();
        final int imageHeight = image.getIconHeight();
        final Dimension d = getSize();
        final int x = (d.width - imageWidth)/2;
        final int y = (d.height - imageHeight)/2;
        g.drawImage(image.getImage(), x, y, null, null);
        super.paint(g);
    }
};
table.setOpaque(false);

final JScrollPane sp = new JScrollPane(table);

final JFrame f = new JFrame();
f.getContentPane().add(sp);
f.setSize(200,200);
f.setVisible(true);

Not sure if this is what you need, but have a look at the Substance Look and Feel which supports watermarks: https://substance.dev.java.net/docs/watermarks.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!