JTable Custom Renderer 不改变颜色

JTable Custom Renderer not changing colors

我正在尝试编写自定义渲染器以根据内部值更改我的 JTable 中的文本颜色。

这是我目前的代码。我试图尽可能地简化它,而不是实际更改代码太多:

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;

public class Main {

public static String[][] resultsArray;
public static JTable results;
public static JScrollPane resultScrollPane;

public static void main(String[] args) {
    resultsArray = new String[][]{{"123456", "192.168.4.16", "3.4/01.73.10", "1x6216", "109986 MB", "Fail", "2xSSD480", "6xHDD2GB", "Fail", "Fail"}};

    resultScrollPane = new JScrollPane();
    JFrame f = new JFrame("Table with colors");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new BorderLayout());
    f.add(resultScrollPane,BorderLayout.CENTER);
    f.setSize(new Dimension(60,255));

    setTableResults(resultsArray);

    f.setVisible(true);
}

public static void setTableResults(String[][] result) {
    Object[] columnNames = {
            "Serial Number",
            "IP Address",
            "BIOS/IPMI-Firmware",
            "CPUs",
            "Memory",
            "DCMS",
            "SSDs",
            "HDDs",
            "Network AOC",
            "AOC"
    };
    TableModel model = new DefaultTableModel(result, columnNames);
    results = new JTable(model);
    results.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            String data = (String) value;
            switch (column) {
                case 0, 1 -> c.setForeground(Color.BLACK);
                case 2 -> c.setForeground((data.contains("3.4") && data.contains("01.73.10")) ? Color.BLACK : Color.RED);
                case 3 -> c.setForeground((data.split("x")[0].equalsIgnoreCase("2")) ? Color.BLACK : Color.RED);
                case 4 -> c.setForeground((Integer.parseInt(data.split(" ")[0]) == 200000) ? Color.BLACK : Color.RED);
                case 5 -> c.setForeground((data.equalsIgnoreCase("ok")) ? Color.BLACK : Color.RED);
                case 6 -> c.setForeground((Integer.parseInt(data.split("x")[0]) == 2) ? Color.BLACK : Color.RED);
                case 7 -> c.setForeground((Integer.parseInt(data.split("x")[0]) == 6) ? Color.BLACK : Color.RED);
            }
            return c;
        }
    });
    resultScrollPane.setViewportView(results);
    results.repaint();
    }
}

问题在于,代码实际上不起作用。所有单元格都具有标准的黑色字体颜色。我尝试添加 .repaint() 以确保 JTable 在显示之前得到更新,但这并没有改变任何东西。

有人知道我的问题是什么吗?

默认情况下,与列关联的 class 是 Object,如 getColumnClass() 的文档中所指定:

Returns Object.class regardless of columnIndex.

因此您可以像这样更改代码以使其工作:

results.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {