java - JTable 中选定的行未突出显示

标签 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 函数中做到这一点吗?

最佳答案

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/

相关文章:

java - 将 JScroll Pane 缩小到与 JTable 相同的高度

java - 推土机的多态性

java - JComboBox as Jtable CellEditor with Overriden stopCellEditing modifies wrong table cell

java - 无法将资源添加到maven中的jar中

java - Java Swing 中 JTextfield 中的控制空格、数字、特殊字符、字符串

java - java类的大小会影响应用程序的性能吗

java - 使用 Netty 或其他轻量级 NIO 库的请求-响应

java - 尽管设置了必要的属性,列表项也不会保持选中状态

java - 我应该把 public static void main(String[] args) 方法放在哪里?

java - 如何在 Swing 中为 JTable 提供分页支持?