自定义 TableCellRenderer 不起作用(表格行渲染)
我正在尝试渲染 jtable 的特定行(它应该有不同的背景颜色并且应该有粗体字符)。
我发现了几个有关如何实现自定义 TableCellRenderer
的问题 (此处 和 此处)和教程如何使用表 和这个(表格行渲染 )。
我使用 JInternalFrame
来显示 JTable
。我尝试实现这两种解决方案,但 getCellRenderer
和 prepareRenderer
都没有被调用。调试时,我可以看到新的 jtable 正在创建,但是,方法内的断点没有被调用。
我的代码如下所示:
this.add(createData(model));
private JComponent createData(ProfitLossTableModel model) {
JTable table = new JTable(model) {
public TableCellRenderer getCellRenderer(int row, int column) {
if ((row == 0) && (column == 0)) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(JLabel.RIGHT);
renderer.setBackground(Color.red);
return renderer;
} else {
return super.getCellRenderer(row, column);
}
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.changeSelection(0, 0, false, false);
table.setAutoCreateRowSorter(true);
return new JScrollPane(table);
}
如您所见,模型是我对 AbstractTableModel 的实现。
当然,我错过了一些东西。我尝试调用 repaint()
来查看是否发生了某些情况,但什么也没发生。
我还尝试将 JPane 放入 JInternalFrame 中,然后将 JScrollPane 添加到其中,但也没有发生任何情况。
有什么帮助吗?
蒂亚,
鲍勃
I'm trying to render an specific row of my jtable (it should have a different background color and it should have bold characters).
I found several questions regarding how to implement a custom TableCellRenderer
(here and here) and the tutorial How to Use Tables and this one (Table Row Rendering).
I'm using a JInternalFrame
to display the JTable
. I tried to implement both solutions but neither getCellRenderer
nor prepareRenderer
are being called. When debugging, I can see my new jtable being created, but, my breakpoint inside the methods aren't called.
My code looks like:
this.add(createData(model));
private JComponent createData(ProfitLossTableModel model) {
JTable table = new JTable(model) {
public TableCellRenderer getCellRenderer(int row, int column) {
if ((row == 0) && (column == 0)) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(JLabel.RIGHT);
renderer.setBackground(Color.red);
return renderer;
} else {
return super.getCellRenderer(row, column);
}
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.changeSelection(0, 0, false, false);
table.setAutoCreateRowSorter(true);
return new JScrollPane(table);
}
As you can see, model is my implementation of a AbstractTableModel
.
Of course that I'm missing something. I tried to call repaint()
to see if something happened, but nothing occurred.
I also tried to put a JPane inside my JInternalFrame and them add the JScrollPane into it, but nothing occurred also.
Any help?
TIA,
Bob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如概念:编辑器和渲染器中所讨论的,“表调用表模型的
getColumnClass
方法,该方法获取列单元格的数据类型。”您应该验证AbstractTableModel
的实现是否返回合适的值,如 示例。附录:
Rob Camick 的表格行渲染重写
prepareRenderer()
的方法似乎很合适。As discussed in Concepts: Editors and Renderers, "the table invokes the table model's
getColumnClass
method, which gets the data type of the column's cells." You should verify that your implementation ofAbstractTableModel
returns a suitable value, as suggested in this example.Addendum:
Rob Camick's Table Row Rendering approach that overrides
prepareRenderer()
seems apropos.