JXTable background image don't renders

181 views Asked by At

I am trying to generate a JXTable with a background image ( a text would be fine as well). Here is my extended JXTable class:

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

The image don't shows up - I only see white space. Any idea?

2

There are 2 answers

0
Thomas On BEST ANSWER

For future reference:

The problem seems to be that the table itself is not rendered transparently. Setting the table itself to opaque = false helps.

0
Robin On

For SwingX the recommended way to for example use an opaque component for rendering is to use the Highlighter interface. So instead of overriding the prepareRenderer method, it is recommended to write your Highlighter and use the JXTable#setHighlighters method to set in on the table