JTable Cell Renderer 仅在获得焦点时更改前景色
JTable CellRenderer changes foreground color only when focused
我正在尝试实现一个根据整数值改变颜色的 cellrenderer。但是,只有在聚焦时,单元格的颜色才会发生变化。见下图。
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
switch (column) {
case 1:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
case 3:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
}
if (column == 4 && (Integer)value > 0) {
setForeground(Color.green);
} else {
setForeground(Color.red);
}
return this;
}
我猜你的 table 没有触发正确的更新事件,所以没有重绘单元格。
包含所有给定代码但无法重现问题的 MRE:
import java.awt.*;
import javax.swing.*;
class Code extends javax.swing.table.DefaultTableCellRenderer {
public static void main(String[] args) throws Throwable {
java.awt.EventQueue.invokeLater(Code::go);
}
private static void go() {
JTable table = new JTable(
new Object[][] { { 0, 1, 2, 3, 4 }, { 1, 2, 3, 4, 5 } },
new Object[] { "0", "1", "2", "3", "3" }
);
table.setDefaultRenderer(Object.class, new Code());
JFrame frame = new JFrame("Table");
frame.add(table);
frame.pack();
frame.setVisible(true);
new Timer(1000, event -> {
table.setValueAt(
-(Integer)table.getValueAt(0, 4),
0, 4
);
}).start();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
switch (column) {
case 1:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
case 3:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
}
if (column == 4 && (Integer)value > 0) {
setForeground(Color.green);
} else {
setForeground(Color.red);
}
return this;
}
}
我正在尝试实现一个根据整数值改变颜色的 cellrenderer。但是,只有在聚焦时,单元格的颜色才会发生变化。见下图。
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
switch (column) {
case 1:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
case 3:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
}
if (column == 4 && (Integer)value > 0) {
setForeground(Color.green);
} else {
setForeground(Color.red);
}
return this;
}
我猜你的 table 没有触发正确的更新事件,所以没有重绘单元格。
包含所有给定代码但无法重现问题的 MRE:
import java.awt.*;
import javax.swing.*;
class Code extends javax.swing.table.DefaultTableCellRenderer {
public static void main(String[] args) throws Throwable {
java.awt.EventQueue.invokeLater(Code::go);
}
private static void go() {
JTable table = new JTable(
new Object[][] { { 0, 1, 2, 3, 4 }, { 1, 2, 3, 4, 5 } },
new Object[] { "0", "1", "2", "3", "3" }
);
table.setDefaultRenderer(Object.class, new Code());
JFrame frame = new JFrame("Table");
frame.add(table);
frame.pack();
frame.setVisible(true);
new Timer(1000, event -> {
table.setValueAt(
-(Integer)table.getValueAt(0, 4),
0, 4
);
}).start();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
switch (column) {
case 1:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
case 3:this.setFont(this.getFont().deriveFont(Font.BOLD)); break;
}
if (column == 4 && (Integer)value > 0) {
setForeground(Color.green);
} else {
setForeground(Color.red);
}
return this;
}
}