JTable TableCellRenderer 没有正确着色
JTable TableCellRenderer not coloring correctly
我用我的自定义 DefaultTableCellRenderer
创建了一个简单的 JTable
。它本身运行良好(为最后一列着色)。但是一旦我 select 一行或 filter/unfilter 它,该行就会被着色,即使它根本不应该被着色。
我的渲染器:
public class StatusCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
table.convertRowIndexToModel(row), col);
DataTableModel model = (DataTableModel) table.getModel();
String data = model.getValueAt(table.convertRowIndexToModel(row), col).toString();
if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(Color.GREEN);
}
if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(new Color(255, 51, 51));
}
return c;
}
}
它最初的样子(以及它应该看起来的样子):
并且在 selecting 2 行(顶部和底部)之后:
如您所见,有几行是绿色的,根本不应该着色。更令人不安的是,我只 select 编辑了绿色块的顶部和底部行,这意味着它也会自动为中间的行着色。
我怎样才能停止这种行为并只为第一张图片中显示的行着色?
接受的答案对我解决问题很有帮助,这是最终代码:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col));
DataTableModel model = (DataTableModel) table.getModel();
String data = model.getValueAt(table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col))
.toString();
if (!isSelected) {
if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(Color.GREEN);
} else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(new Color(255, 51, 51));
} else {
c.setBackground(Color.WHITE);
}
} else {
c.setBackground(c.getBackground());
}
return c;
}
如果单元格 selected,它会显示蓝色,如果没有,则根据值
显示白色、绿色或红色
由于渲染器组件将被重复使用,考虑在没有条件匹配时设置默认颜色:
if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(Color.GREEN);
}
else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(new Color(255, 51, 51));
}
else {
c.setBackground(Color.GRAY.brighter());
}
我用我的自定义 DefaultTableCellRenderer
创建了一个简单的 JTable
。它本身运行良好(为最后一列着色)。但是一旦我 select 一行或 filter/unfilter 它,该行就会被着色,即使它根本不应该被着色。
我的渲染器:
public class StatusCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
table.convertRowIndexToModel(row), col);
DataTableModel model = (DataTableModel) table.getModel();
String data = model.getValueAt(table.convertRowIndexToModel(row), col).toString();
if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(Color.GREEN);
}
if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(new Color(255, 51, 51));
}
return c;
}
}
它最初的样子(以及它应该看起来的样子):
并且在 selecting 2 行(顶部和底部)之后:
如您所见,有几行是绿色的,根本不应该着色。更令人不安的是,我只 select 编辑了绿色块的顶部和底部行,这意味着它也会自动为中间的行着色。
我怎样才能停止这种行为并只为第一张图片中显示的行着色?
接受的答案对我解决问题很有帮助,这是最终代码:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col));
DataTableModel model = (DataTableModel) table.getModel();
String data = model.getValueAt(table.convertRowIndexToModel(row), table.convertColumnIndexToModel(col))
.toString();
if (!isSelected) {
if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(Color.GREEN);
} else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(new Color(255, 51, 51));
} else {
c.setBackground(Color.WHITE);
}
} else {
c.setBackground(c.getBackground());
}
return c;
}
如果单元格 selected,它会显示蓝色,如果没有,则根据值
显示白色、绿色或红色由于渲染器组件将被重复使用,考虑在没有条件匹配时设置默认颜色:
if (col == 3 && data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(Color.GREEN);
}
else if (col == 3 && !data.equalsIgnoreCase("successful") && !data.isEmpty()) {
c.setBackground(new Color(255, 51, 51));
}
else {
c.setBackground(Color.GRAY.brighter());
}