【问题标题】:Cells with centered value and background based on value基于值的居中值和背景的单元格
【发布时间】:2020-08-06 07:18:43
【问题描述】:

这是我的问题 我读取数据库并通过渲染器显示数据。显示的数据必须居中,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;
     }
}

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

【问题讨论】:

    标签: java rendering


    【解决方案1】:

    这里有一个简短的 sn-p 演示我认为您正在努力实现的目标:

    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。此外,如果您只想在特定的类类型上使用自定义呈现,例如仅在 String 对象上,您可以使用注释掉的行而不是遍历表的所有列。 当然,您可以根据需要为CustomRenderer 添加更多限制。

    【讨论】:

    • 清晰完美。谢谢
    猜你喜欢
    • 2011-11-14
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2021-02-12
    • 2016-07-25
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多