如何使JTable中的文本加粗,我尝试了以下方法,它可以改变颜色,但是在使文本加粗方面我做错了什么。
class ColourRender extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable tblData,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component cellComponent = super.getTableCellRendererComponent(
tblData, value, isSelected, hasFocus, row, column);
if (tblData.getValueAt(row, 3).equals("M")) {
cellComponent.setForeground(forMen);
} else {
cellComponent.setForeground(forWomen);
}
return cellComponent;
}
class boldRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable tblData,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cellComponent = super.getTableCellRendererComponent(
tblData, value, isSelected, hasFocus, row, column);
if (tblData.getValueAt(row, 1).equals(bib)) {
cellComponent.setFont(cellComponent.getFont().deriveFont(
Font.BOLD));
} else {
cellComponent.setFont(cellComponent.getFont().deriveFont(
Font.PLAIN));
}
return cellComponent;
}
}
}
这是在与表格相关的类中的内容。
tblData.getColumn("Bib no").setCellRenderer(new CustomRenderer());
tblData.getColumn("M/F").setCellRenderer(new CustomRenderer());
cellComponent.setForeground(null);
应该改为cellComponent.setForeground(myJTable.getForeground())
,值的测试也不太妥当,等等...,请看这个链接。 - mKorbel