0

I have a JTable, and I am trying to insert an image behind the JTable as a Watermark

tblMainView= new JTable(dtModel){

        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(true);
        return c;
        }
        ImageIcon image = new ImageIcon( "images/watermark.png" );

          public void paint( Graphics g )
        {
        // First draw the background image - tiled 
        Dimension d = getSize();
        for( int x = 0; x < d.width; x += image.getIconWidth() )
        for( int y = 0; y < d.height; y += image.getIconHeight() )
        g.drawImage( image.getImage(), x, y, null, null );
        // Now let the regular paint code do it's work
        super.paint(g);
        }


        public boolean isCellEditable(int rowIndex, int colIndex) {
          return false;
        }
        public Class getColumnClass(int col){
            if (col == 0)  
            {  
            return Icon.class;  
            }else if(col==7){
                return String.class;
            }
        else
            return String.class;  

        }   
        public boolean getScrollableTracksViewportWidth() {
            if (autoResizeMode != AUTO_RESIZE_OFF) {
                if (getParent() instanceof JViewport) {
                return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
                }
            } 
            return false;
            }

    };

The above is the c ode of my JTable, but the watermark in not visible; let me add that later I place this JTable in a JScrollPane and JSplitPane.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Asghar
  • 2,336
  • 8
  • 46
  • 79

3 Answers3

3

there are some mistakes

really not good idea use paint(Graphics g) for Swing JComponents, paint() is for AWT code, for Swing is there paintComponent(Graphics g), usage of paint(Graphics g) in Swing would have shows unexpected output to the Swing GUI,

really not good idea paint(Graphics g)for AWT code or paintComponent(Graphics g) for Swing code inside any of Renderer

you have to prepare JTable's BackGroung as is demonstrated here TableWithGradientPaint

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

Two possible solutions, but I don't know which one. :D I think the first approach will have the highest chance of success.

First approach

Override your paintComponent(Graphics g) method:

public void paintComponent(Graphics g)
{
    //First super
    super.paintComponent(g);

    g.drawImage(0, 0, getWidth(), getHeight());
}

Second approach

Set your JTable opaque to false: table.setOpaque(false);
Override your paintComponent(Graphics g) method:

public void paintComponent(Graphics g)
{
    //First draw
    g.drawImage(0, 0, getWidth(), getHeight());

    super.paintComponent(g);
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • simple and short, I think there are "must be" `setOpaque(false)` in both cases for TableRowSelection, +1 – mKorbel Jul 20 '11 at 15:22
1

Try calling super.paint(g) before you paint your watermark, the JTable is probably painting over your image.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141