Java 改变JTable中特定单元格的颜色

Java 改变JTable中特定单元格的颜色,java,swing,colors,jtable,cell,Java,Swing,Colors,Jtable,Cell,我正在尝试更改JTable中列中一个或多个特定单元格的颜色。我在这里看到的答案都是指这种特殊的方法 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component y = super.getTableCellRendererComponent(table, val

我正在尝试更改JTable中列中一个或多个特定单元格的颜色。我在这里看到的答案都是指这种特殊的方法

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    Component y = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    y.setBackground(new Color(255,0,0));

    return y;
}
但问题是,我不明白这是如何工作的,在我的另一个类中,我有一个字符串Jtable,我想根据字符串值更改某些单元格的颜色,但是我找到的解决方案只允许我更改Jtable中整列单元格的颜色,而不允许更改特定单元格的颜色

我有一个字符串表,我想根据字符串值改变某些单元格的颜色

所有单元格都使用相同的渲染器,因此每次都需要重置背景

在自定义渲染器代码中需要if条件。比如:

if (!isSelected)
    if (value.equals(...))
        y.setBackground(new Color(255,0,0));
    else
        y.setBackground(table.getBackground())
我有一个字符串表,我想根据字符串值改变某些单元格的颜色

所有单元格都使用相同的渲染器,因此每次都需要重置背景

在自定义渲染器代码中需要if条件。比如:

if (!isSelected)
    if (value.equals(...))
        y.setBackground(new Color(255,0,0));
    else
        y.setBackground(table.getBackground())

您可以使用DefaultTableCellRenderer为JTable中的备用行着色

table.setDefaultRenderer(Object.class, new TableCellRenderer(){
    private DefaultTableCellRenderer DEFAULT_RENDERER =  new DefaultTableCellRenderer();

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(isSelected){
                    c.setBackground(Color.YELLOW);
                }else{
                if (row%2 == 0){
                    c.setBackground(Color.WHITE);

                }
                else {
                    c.setBackground(Color.LIGHT_GRAY);
                }     }

       //Add below code here
                return c;
            }

        });
如果要使用特定行的值为行着色,则可以使用类似的方法。将这些行添加到上面

if(table.getColumnModel().getColumn(column).getIdentifier().equals("Status")){//Here `Status` is column name
    if(value.toString().equals("OK")){//Here `OK` is the value of row

        c.setBackground(Color.GREEN);
    }   
}

您可以使用DefaultTableCellRenderer为JTable中的备用行着色

table.setDefaultRenderer(Object.class, new TableCellRenderer(){
    private DefaultTableCellRenderer DEFAULT_RENDERER =  new DefaultTableCellRenderer();

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if(isSelected){
                    c.setBackground(Color.YELLOW);
                }else{
                if (row%2 == 0){
                    c.setBackground(Color.WHITE);

                }
                else {
                    c.setBackground(Color.LIGHT_GRAY);
                }     }

       //Add below code here
                return c;
            }

        });
如果要使用特定行的值为行着色,则可以使用类似的方法。将这些行添加到上面

if(table.getColumnModel().getColumn(column).getIdentifier().equals("Status")){//Here `Status` is column name
    if(value.toString().equals("OK")){//Here `OK` is the value of row

        c.setBackground(Color.GREEN);
    }   
}

你的问题似乎有点言之过早。您需要学习如何使用TableCellRenderer,例如通过扩展DefaultTableCellRenderer或AbstractTableCellRenderer。如果您阅读相应的API,并更努力地搜索此站点,您将找到至少需要进行初步尝试的所有信息。您的问题似乎有点过早。您需要学习如何使用TableCellRenderer,例如通过扩展DefaultTableCellRenderer或AbstractTableCellRenderer。如果您阅读相应的API,并更努力地搜索此网站,您将找到至少在这方面进行初步尝试所需的所有信息。好吧,吹毛求疵(并不是真的帮助OP,因为他/她似乎需要后退一步,从更基本的东西开始;-):仅检查所选状态不够好,可能有更多状态需要区分(如dropLocation、focused和editable等)。-偏袒我-是在调用super之前检查自己的情况,如果不感兴趣,则将颜色设置为nullwell,挑剔(并不是真的帮助OP,因为他/她似乎需要后退一步,从更基本的东西开始;-):仅检查所选状态不够好,可能有更多状态需要区分(如dropLocation,focused&&editable,…)-Biasted me-是在调用super之前检查自己的情况,如果不感兴趣,请将颜色设置为null