JTable行选择后台问题。
我有一个 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(false);
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;
}
};
tblMainView.setOpaque(false);
每件事都工作正常。但是当我选择一行时,行数据隐藏。它显示我的行,如
我希望结果与此相同, dtModel 是我名为 tblMainView 的 JTable 的 deafultTableModel
I have a JTable and to set a picture as background in JTable and other properties i used this code.
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(false);
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;
}
};
tblMainView.setOpaque(false);
Every thing is working correctly. But when i select a row, the row data hides.it shows my row like
i want the result same like this,
dtModel is the deafultTableModel for my JTable named tblMainView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
推荐重写
prepareRenderer()
为整个表行进行自定义渲染的方法,但您不能让它完成所有操作。特别是的默认渲染器 Icon
应该做你想要的事情,并且根本没有理由重写paint()
。附录:仔细观察,您选择的字段显示为空,因为
setOpaque(false)
干扰DefaultTableCellRenderer
API。您复制的示例将不起作用。作为参考,下面的示例覆盖
DefaultTableModel
的getColumnClass()
以获取Icon
和Date
类型的默认渲染器。Overriding
prepareRenderer()
is a recommended way to do custom rendering for an entire table row, but you can't make it do everything. In particular, the default renderer forIcon
should do what you want, and there's no reason to overridepaint()
, at all.Addendum: Looking closer, your selected field appears empty because
setOpaque(false)
interferes with the optimization mentioned in theDefaultTableCellRenderer
API. The example you copied won't work.For reference, the example below overrides the
getColumnClass()
ofDefaultTableModel
to obtain the default renderer for typesIcon
andDate
.