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.