1

我正在尝试生成带有背景图像的 JXTable(文本也可以)。这是我扩展的 JXTable 类:

public class JXTableWithBackground extends JXTable{

    ImageIcon image;
    public JXTableWithBackground(ParticipantTableModel pTableModel, ImageIcon image){
        super(pTableModel);
        this.image=image;
    }
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
        Component c = super.prepareRenderer( renderer, row, column);
        // We want renderer component to be transparent so background image is visible
        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);
    }

图像不显示 - 我只看到空白。任何想法?

4

2 回答 2

1

例如SwingX,使用不透明组件进行渲染的推荐方法是使用Highlighter接口。所以建议不要重写prepareRenderer方法,而是写你的Highlighter并使用JXTable#setHighlighters方法在表上设置

于 2012-01-16T17:11:48.603 回答
1

备查:

问题似乎是表格本身没有透明地呈现。将表本身设置为 opaque = false 会有所帮助。

于 2012-01-16T17:00:02.083 回答