有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

启动时swing Java Jtable行颜色

用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;

    }
}

因此,要进行汇总,最后一行中“不可用”的行确实会变为红色,但只有在表加载后(而不是加载时)才变为红色

任何想法。谢谢。:)


共 (1) 个答案

  1. # 1 楼答案

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

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

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

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

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

    因此,与创建多个渲染器不同,您可能希望查看Table Row Rendering,它提供了一种替代解决方案