[英]Selected row in JTable is not highlighting
好吧,这是我在这里的第一篇文章。 但是我有点沮丧,因为我到处都有搜索,但是什么都没用。 我有JTable
并且代码在if( value.equals("CMAU1294522")
)之后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
函数中,有什么办法可以做到?
我有什么办法可以在已经拥有的PreparRenerere函数中进行操作吗?
您编写的代码获取“值”是错误的。 您总是会得到当前正在渲染的单元格的值。 如果要基于特定值突出显示整个行,则需要对列进行硬编码:
Object value = getModel().getValueAt(row, ???);
另外,看起来您正在对表中的数据进行排序,因此您应该使用getValueAt(...)而不是getModel()。getValueAt(),以便检查排序表中的数据,而不是未排序的数据模型。
请查看“ 表行渲染”中的示例代码以获取可用示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.