具有中心值和基于值的背景的单元格

Cells with centered value and background based on value

这是我的问题 我读取一个数据库并通过渲染器显示数据。显示的数据必须居中并且 bgcolor 必须根据单元格的值更改。

以下是渲染器。第一个 table.getColumnModel() 显示居中值,第二个调用客户渲染器来更改颜色。如果我删除第二次调用的评论,我得到 bgcolor 但不是中心值,反之亦然

for (int row = 0; row < table.getColumnCount(); row++) {
    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(JLabel.CENTER);
    table.getColumnModel().getColumn(row).setCellRenderer(centerRenderer);
    //table.getColumnModel().getColumn(row).setCellRenderer(new CustomRenderer()); 
}

这是自定义渲染器

class CustomRenderer extends DefaultTableCellRenderer
{
   public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
   {
       Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
       if(table.getValueAt(row, column).equals("A")){
           cellComponent.setBackground(Color.YELLOW);
           
       } else if(table.getValueAt(row, column).equals("B")){
           cellComponent.setBackground(Color.GRAY);
       }
       else {cellComponent.setBackground(Color.white);}
           return cellComponent;
     }
}

如何获得彩色和中心单元格? 比你 爸爸

这里有一个简短的片段,展示了我认为您正在努力实现的目标:

public class Testing {

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Testing();
        }
    });

}

public Testing() {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    String[] colNames = { "Col1", "Col2" };
    Object[][] data = { { "A", "B" }, { "B", "C" } };

    JTable table = new JTable(data, colNames);
    CustomRenderer renderer = new CustomRenderer();
    renderer.setHorizontalAlignment(JLabel.CENTER);
    // table.setDefaultRenderer(String.class, renderer); // if you want to only
    // color and center all strings
    for (int col = 0; col < table.getColumnCount(); col++) {
        // if you want to color and center every entry of the table
        table.getColumnModel().getColumn(col).setCellRenderer(renderer);
    }
    panel.add(table);
    frame.add(panel);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

class CustomRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
        cellComponent.setBackground(Color.white); // white if not "A" or "B"
        if (value instanceof String) {
            String temp = (String) value;
            if (temp.equals("A")) {
                cellComponent.setBackground(Color.YELLOW);
            } else if (temp.equals("B")) {
                cellComponent.setBackground(Color.GRAY);
            }
        }
        return cellComponent;
    }
}
}

结果将如下所示:

请注意,您的自定义渲染器只需创建一次,而不是在每个循环中创建。您的代码中的问题是,您没有使用自己的渲染器,而是使用了 DefaultTableCellRenderer。此外,如果您只想在特定 class 类型上使用自定义呈现,例如仅在 String 对象上,您可以使用注释掉的行而不是遍历 table 的所有列。 当然,您可以根据需要对 CustomRenderer 添加更多限制。