java 如何在JTable中添加WaterMark
我有一个 JTable
,我试图在 JTable
后面插入一个图像作为水印
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;
}
};
以上是 c 我的 JTable
的颂歌,但水印不可见;让我补充一下,稍后我将此 JTable
放置在 JScrollPane
和 JSplitPane
中。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两种可能的解决方案,但我不知道哪一种。 :DI认为第一种方法成功的机会最高。
第一种方法
重写
paintComponent(Graphics g)
方法:第二种方法
将 JTable 不透明设置为 false:
table.setOpaque(false);
重写您的
paintComponent(Graphics g)
方法: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:Second approach
Set your JTable opaque to false:
table.setOpaque(false);
Override your
paintComponent(Graphics g)
method:有一些错误
确实不是个好主意,对于
Swing JComponents
使用paint(Graphics g)
,paint()
用于 AWT 代码,对于 Swing 是paintComponent(Graphics g)
,在 Swing 中使用paint(Graphics g)
会向 Swing GUI 显示意外的输出,这确实不是一个好主意
paint(Graphics g)
AWT 代码或paintComponent(Graphics g)
对于任何Renderer
内的 Swing 代码,您必须准备 JTable 的 BackGroung,如此处所示 TableWithGradientPaint
there are some mistakes
really not good idea use
paint(Graphics g)
forSwing JComponents
,paint()
is for AWT code, for Swing is therepaintComponent(Graphics g)
, usage ofpaint(Graphics g)
in Swing would have shows unexpected output to the Swing GUI,really not good idea
paint(Graphics g)
for AWT code orpaintComponent(Graphics g)
for Swing code inside any ofRenderer
you have to prepare JTable's BackGroung as is demonstrated here TableWithGradientPaint
在绘制水印之前尝试调用 super.paint(g) ,
JTable
可能正在您的图像上绘制。Try calling
super.paint(g)
before you paint your watermark, theJTable
is probably painting over your image.