我创建了一些costum单元格渲染来改变我的jtable的外观,我改变了一些行的颜色和字体,我试图做同样但它不起作用,这是我的代码:
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
int firstRow = 0;
int lastRow = jTable1.getRowCount() - 1;
if (row == lastRow) {
((JComponent) c).setBackground(new Color(218, 244, 247));
//((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
} else if (row == firstRow) {
((JComponent) c).setBackground(new Color(218, 244, 247));
((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
((JComponent) c).setForeground(new Color(11, 114, 126));
} else if (row == 5) {
((JComponent) c).setBackground(new Color(218, 244, 247));
((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
} else {
((JComponent) c).setBackground(jTable1.getBackground());
}
return c;
}
这是我遇到问题的时候:
((JComponent)c)。setForeground(new Color(11,114,126));
if (row == firstRow) {
((JComponent) c).setBackground(new Color(218, 244, 247));
((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
((JComponent) c).setForeground(new Color(11, 114, 126));}
由于
答案 0 :(得分:1)
,我改变了一些行的颜色和字体,我试着这样做但是 它不起作用,这是我的代码:
完全针对prepareRenderer
的工作,您的问题必须在其余代码的某处(我们可以看到您之前的帖子),以便更快地发布SSCCE,简短,可运行,可编辑,仅约JFrame
和JTable
prepareRenderer
但必须覆盖convertXxxToModel
,因为ColumnModel
could be reordered and/or rows can be sorted or filtered,在这种情况下JTables view
中的索引与索引XxxTableModel
不同,然后XxxRenderer
无法正常工作