【发布时间】:2013-05-28 18:53:28
【问题描述】:
我想更改 JTable 中整行的颜色。
我定义了 JTable:
JTable table = new JTable(data, columnNames);
其中数据,columnNames 是字符串表。
最常见的方法是编写自己的类:
public class StatusColumnCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
//Cells are by default rendered as a JLabel.
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
//Get the status for the current row.
l.setBackground(Color.GREEN);
//Return the JLabel which renders the cell.
return l;
}
}
然后调用:
this.table.getColumnModel().getColumn(0).setCellRenderer(new StatusColumnCellRenderer());
但它不起作用。我做错了什么?
【问题讨论】:
-
这应该可以。问题是你没有向我们展示什么。发布SSCCE
-
我在帖子中添加了整个代码。
标签: java swing jtable row tablecellrenderer