【问题标题】:My columns in the JTable don't get all a background color我在 JTable 中的列没有得到所有背景颜色
【发布时间】:2022-11-12 13:58:18
【问题描述】:

我表中的列并非都具有背景颜色。如果我在表格中使用复选框,它不会获得背景颜色。

我用这段代码来设置背景:

participantsTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        c.setBackground(row % 2 == 0 ? new Color(230, 230, 230): Color.WHITE);
        return this;
    }
});

我尝试在互联网上搜索解决方案,但无济于事。我对 JTabel 不太熟悉,以至于我自己可以提出错误。

这是不应该发生的事情:

所以它不应该是这样的,但是复选框的背景应该和它左边的列一样。

我做错了什么,我该如何解决这个问题?

【问题讨论】:

    标签: java jtable tablecellrenderer


    【解决方案1】:

    复选框的背景应与其左侧的列相同。

    不同的数据类型使用不同的渲染器。

    在您的情况下,您还需要为Boolean.class 自定义渲染器。

    或者,您可以覆盖JTableprepareRenderer(...) 方法。

    一个基本的例子是:

    JTable table = new JTable( model )
    {
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
        {
            Component c = super.prepareRenderer(renderer, row, column);
    
            //  Alternate row color
    
            if (!isRowSelected(row))
                c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
    
            return c;
        }
    };
    

    在确定单元格的渲染器后调用此方法,因此它将适用于表的所有列。

    查看Table Row Rendering 了解更多信息和使用此方法进行自定义渲染的示例。

    【讨论】:

      【解决方案2】:

      我使用了你的代码,但我仍然有同样的问题。

      在这里你可以看到我的整个代码:

      public void init() {
              //create new table
              DefaultTableModel model = new DefaultTableModel() {
                  public Class getColumnClass(int column) {
                      switch(column) {
                          case 0: return String.class;
                          case 1: return String.class;
                          case 2: return Boolean.class;
                          default: return Boolean.class;
                      }
                  }
              };
              participantsTable.setModel(model);
              
              //row color
              JTable table = new JTable(model) {
                  public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                      Component c = super.prepareRenderer(renderer, row, column);
      
                      //  Alternate row color
                      if (!isRowSelected(row))
                          c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
      
                      return c;
                  }
              };
              
              //values
              Object[] cols = {"Name", "Firstname", "03.03.22", "03.03.22"};
              Object[][] data = {
                  {"Name1","FirstName1",true,false},
                  {"Name2","FirstName2",true,false},
                  {"Name3","FirstName3",false,true},
                  {"Name4","FirstName4",false,false}
              };
              model.setDataVector(data, cols);
              
              participantsTable.setRowHeight(25);
              participantsTable.setFont(new Font("Arial", Font.PLAIN, 13));
              participantsTable.getTableHeader().setForeground(new Color(255, 157, 85));
              participantsTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 13));
          }
      

      【讨论】:

        猜你喜欢
        • 2023-01-29
        • 2015-10-24
        • 2022-01-24
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        • 1970-01-01
        • 2019-09-29
        相关资源
        最近更新 更多