문제

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?

도움이 되었습니까?

해결책

For future reference:

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top