嗯,这是我在这里发表的第一篇文章。但我有点沮丧,因为我到处搜索但没有任何效果。我有 JTable
,代码在 if (value.equals("CMAU1294522")
) 之后按预期工作。但是只有一个单元格显示方框。我用鼠标单击特定行,我希望整行显示浅灰色(我认为这是标准的)。
table = new JTable(sorter) {
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, col);
UIManager.put("Table.editRow", new javax.swing.plaf.ColorUIResource(Color.YELLOW));
if(editingRow ==1){
table.setSelectionBackground(Color.red);
}
if (getSelectedRow() == row) {
table.setSelectionBackground(Color.red);
}
if (value.equals("CMAU1294522")) {
comp.setBackground(Color.red);
} else if (value.equals("PNCT")) {
comp.setBackground(Color.green);
} else if (NewJApplet.contReady.containsKey(value)) {
comp.setBackground(Color.CYAN);
} else if (NewJApplet.badCont.containsKey(value)) {
comp.setBackground(Color.red);
} else {
comp.setBackground(Color.white);
}
return comp;
}
有什么方法可以在我已有的 prepareRenderer
函数中做到这一点吗?
最佳答案
I there any way to do it in PreparRenerere function I already have?
您获取“值”的代码是错误的。您始终可以获得当前正在渲染的单元格的值。如果您想根据特定值突出显示整行,则需要对该列进行硬编码:
Object value = getModel().getValueAt(row, ???);
此外,看起来您正在对表中的数据进行排序,因此您应该使用 getValueAt(...) 而不是 getModel().getValueAt(),因此您检查排序表中的数据,而不是数据在未排序的模型中。
查看 Table Row Rendering 中的示例代码一个工作示例。
关于java - JTable 中选定的行未突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251286/