如何增加上的JTable图像背景,滚动的JTable时不会滚动 [英] How to add image background on JTable , that does not scroll when scrolling JTable
问题描述
我需要添加背着我JTable中的图像的背景下,不应该scrole倒边滚动我的JTable。目前我添加了一个形象behing我的JTable。使用paint方法。
I need to add an image background behind my JTable, should not scrole down while scrolling my JTable. currently i have added a Image behing my JTable. using the paint method.
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);
}
问题是。此JTable是在JScrollPane中。和滚动窗格时。还向下滚动图像。并且重复在每个涡卷的图像。
problem is that. This JTable is at JScrollPane. and when scrolling the pane. also scrolls down the image. and repeats the image at each scroll.
有没有什么办法来限制只背景滚动。谢谢
is there any way to restrict scroll on background only. thanks
推荐答案
漆上的 JScrollPane的
,而不是背景。您还需要使双方的的JTable
和单元格渲染器采用透明 setOpaque(假)
。的(和压倒一切的时候使用的paintComponent
法)。的
Paint the background on the JScrollPane
instead. You also need to make both the JTable
and the cell renderer transparent by using setOpaque(false)
. (And use the paintComponent
method when overriding).
在code制作这张截图如下:
The code below produced this screenshot:
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Test");
final BufferedImage image = ImageIO.read(new URL(
"http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
JTable table = new JTable(16, 3) {{
setOpaque(false);
setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {{
setOpaque(false);
}});
}};
frame.add(new JScrollPane(table) {{
setOpaque(false);
getViewport().setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
super.paintComponent(g);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
这篇关于如何增加上的JTable图像背景,滚动的JTable时不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!