Java Jtable 启动时的行颜色

Java Jtable row colour on startup

用 java 做一个图书馆系统项目,我得到了根据字符串改变颜色的行,但是它们只在字符串改变时改变并且在启动时没有检测到正确的字符串。

这是我单击 jMenu 项显示书籍时的代码:

public void displayBooks() {
    // headers for the table
    String[] columns = new String[] { "ISBN", "Title", "Author", "Publisher", "Pub Date", "Status" };

    Object[][] data = new Object[booksList.size()][6];

    for (int i = 0; i < booksList.size(); i++) {
        Book book = booksList.get(i);
        data[i][0] = book.getIsbn();
        data[i][1] = book.getTitle();
        data[i][2] = book.getAuthor();
        data[i][3] = book.getPublisher();
        data[i][4] = book.getPudDate();
        data[i][5] = book.getStatus();
        System.out.println(book.getStatus());
    }

    table = new JTable(data, columns);
    table.setDefaultRenderer(Object.class, new MyCellRenderer());
    this.getContentPane().removeAll();

    TableColumn tableStatus = table.getColumnModel().getColumn(5);
    JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addItem("Available");
    comboBox.addItem("Unavailable");
    tableStatus.setCellEditor(new DefaultCellEditor(comboBox));

    this.getContentPane().add(new JScrollPane(table));
    this.revalidate();

}

现在我的单元格渲染器:

public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
    public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value,
            boolean isSelected, boolean hasFocus, int row, int column) 
    {
        final java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        Object rowValue = table.getValueAt(row, 5);

        Object[][] data = new Object[booksList.size()][6];

        for (int i = 0; i < booksList.size(); i++) {
            Book book = booksList.get(i);
            data[i][0] = book.getIsbn();
            data[i][1] = book.getTitle();
            data[i][2] = book.getAuthor();
            data[i][3] = book.getPublisher();
            data[i][4] = book.getPudDate();
            data[i][5] = book.getStatus();
            System.out.println(book.getStatus());

            if (rowValue == "Unavailable"){
                cellComponent.setForeground(Color.BLACK);
                cellComponent.setBackground(Color.red);;
            }
            else{
                cellComponent.setBackground(Color.white);
                cellComponent.setForeground(Color.black);
            }
            if(isSelected){
                cellComponent.setForeground(table.getSelectionForeground());
                cellComponent.setBackground(table.getSelectionBackground());
            }
        }


        return cellComponent;

    }
}

总结一下,最后一行 "Unavailable" 的行确实会变为红色,但只有在加载 table 之后才会更改,而不是在加载时更改。

任何想法。谢谢。 :)

首先不要使用“==”进行字符串比较。相反,您应该使用 String.equals(...) 方法:

if ("Unavailable".equals( rowValue.toString() )

接下来,您的渲染器代码完全错误。渲染器一次渲染一个单元格。所以如果你有 5 行数据,渲染器将被调用 30 次,因为你有 6 列数据。

我建议您在论坛中搜索扩展 DefaultTableCellRenderer 的其他示例,然后修改这些示例。

但是,创建自定义渲染器的一个问题是您需要为 table 中的每种数据创建自定义渲染器。例如,"date" 通常由自定义日期渲染器渲染,而不是字符串渲染器,因此可以合理地格式化数据。

因此,与其创建多个渲染器,不如看看 Table Row Rendering,它提供了另一种解决方案。