JTable中的选定行不突出显示

时间:2015-05-15 03:57:20

标签: java swing jtable tablecellrenderer tablerowsorter

嗯,这是我在这里的第一篇文章。但我有点沮丧,因为我到处都搜索,但没有任何工作。我有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函数中执行此操作?

1 个答案:

答案 0 :(得分:1)

  

我有没有办法在PreparRenerere函数中做到这一点?

您编码以获得"值"是错的。您始终获得正在呈现的当前单元格的值。如果要根据特定值突出显示整行,则需要对列进行硬编码:

Object value = getModel().getValueAt(row, ???);

此外,看起来您正在对表中的数据进行排序,因此您应该使用getValueAt(...)而不是getModel()。getValueAt(),因此您检查排序表中的数据,而不是数据在未分类的模型中。

查看Table Row Rendering中的示例代码以获取工作示例。

相关问题