I am using a JInternalFrame in that i have added JTable. Now i want to show background image in JTable. so i have added following code in the JScrollPane's customize code.
jScrollPane1 = new javax.swing.JScrollPane(ViewBalanceReportTable) {{
setOpaque(false);
getViewport().setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
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.paintComponent(g);
}
}
but still it is not showing the background image can any one help me on this
Basically, you need to make sure that EVERYTHING that sits on top of the frame is transparent (opaque == false).
The table is a special case, it doesn’t tend to respect the
opaquesetting, because that would be easy. Instead, we can trick it by also using a transparent color.You are AWLAYS better of replacing the content pane if you want to paint to any kind of frame. This will allow you to paint within the content area and not in areas used by things like the frame’s border or menus.
Image Table
Possibly a “simpler” solution would be to render the image directly onto the background the table. This means that the image becomes apart of the table and will scroll with it.
This is a little tricky, as
JTable#paintComponentnot only fills the background, but also renders the tables content.Sticky Viewport
Your other option is to create a custom viewport. This allows you to render the content behind a verity of other components. This will run into the same issue’s you had before. The table and it’s background must be set to transparent.
It also means, that with some clever work, you either have the image “stick” or “follow” the content, depending on what you need.
A lot of it will come down to your actual requirements