JXTable背景图像不渲染

时间:2012-01-16 16:33:31

标签: java swingx jxtable

我正在尝试使用背景图像生成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);
    }

图像没有显示 - 我只看到空白区域。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

供将来参考:

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

答案 1 :(得分:1)

对于SwingX,建议使用不透明组件进行渲染的方法是使用Highlighter接口。因此,建议不要覆盖prepareRenderer方法,而是编写Highlighter并使用JXTable#setHighlighters方法设置表格