【发布时间】:2021-11-16 15:08:08
【问题描述】:
你好,我有问题 我需要将每 10 秒刷新一次的 jtabl 的某些行涂成红色。 也改变颜色的行集。我更新要更改为读取的行集,然后我需要在找到时更改表中的行。 我尝试了表格的单元格渲染器,但没有。 如果有人可以提出建议,我对一切都持开放态度 很想得到一些代码作为例子。 任何方式都会受到欢迎。 我尝试玩这个功能,但我无法添加要更改的行集.. '''
class MyCellRenderer extends DefaultTableCellRenderer {
String separatedVariable;
public MyCellRenderer(String separatedVariable) {
this.separatedVariable = separatedVariable;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
c.setBackground(Color.WHITE);
c.setForeground(Color.BLACK);
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
if (separatedVariable.equals("YOUR VALUE TO GREEN")) {
l.setBackground(Color.GREEN);
return l;
} else {
if (separatedValue.equals("YOUR VALUE TO YELLOW")) {
l.setBackground(Color.YELLOW);
return l;
} else if (separatedValue.equals("YOUR VALUE TO RED")) {
l.setBaground(Color.RED);
return l;
}
}
return c;
}
} '''
【问题讨论】:
标签: java swing colors jtable rows