【问题标题】:Setting rows of different colours in JTable?在JTable中设置不同颜色的行?
【发布时间】:2019-05-22 07:09:47
【问题描述】:

下午好, 我用于渲染 JTable 的渲染类有问题。我想要的是将第 1 到 4 行的背景颜色设置为蓝色,从 5 到 6 设置为橙色,从 18 到 20 设置为红色。但是,它不允许我这样做。我没有收到任何类型的错误,但这段代码只允许我设置这三个条件之一。如果我输入这段代码,表格中唯一弹出的就是最后三行红色,我想要同时显示这三行。

class TeamBold extends DefaultTableCellRenderer {
    private String nombre;

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {

        JLabel parent = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (value.equals(nombre)) {
            parent.setFont(parent.getFont().deriveFont(Font.BOLD)); //Here I just set a certain cell to bold
        }
        for (int i = 0; i<table.getRowCount();i++) {

        }
        if (row >= 0 && row <4) {
            parent.setBackground(Color.BLUE);
            parent.setForeground(Color.WHITE);
        }
        if (row >= 4 && row <6) {
            parent.setBackground(Color.orange);
            parent.setForeground(Color.WHITE);
        } 
        if (row >= 17 && row <19) {
            parent.setBackground(Color.RED);
            parent.setForeground(Color.WHITE);
        } 
        else {
            parent.setBackground(Color.white);
        }
        return parent;
    }

【问题讨论】:

    标签: java swing colors jtable


    【解决方案1】:

    另一个选项是覆盖JTableprepareRenderer(...) 方法。即使您的表格中有字符串、整数和模型中的对象,这种方法也将起作用。查看Table Row Rendering 了解更多信息。

    此建议的基本实现类似于:

    JTable table = new JTable(25, 5)
    {
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
        {
            Component c = super.prepareRenderer(renderer, row, column);
    
            if (isRowSelected(row)) return c;
    
            //  Customize row colors
    
            c.setBackground( getBackground() );
            c.setForeground( getForeground() );
    
            if (row < 4)
            {
                c.setBackground(Color.BLUE);
                c.setForeground(Color.WHITE);
            }
            else if (row < 6)
            {
                c.setBackground(Color.orange);
                c.setForeground(Color.BLACK);
            }
            else if (row >= 17 && row < 20)
            {
                c.setBackground(Color.RED);
                c.setForeground(Color.WHITE);
            }
    
            return c;
        }
    };
    

    您需要使用getVauleAt(...) 方法获取值以确定是否要将单元格设为粗体。

    【讨论】:

      【解决方案2】:

      还有剩余的“其他”块。这是代码。

      JLabel parent = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
      		if (value.equals(nombre)) {
      			parent.setFont(parent.getFont().deriveFont(Font.BOLD));
      		}
      
      		if (row >= 0 && row < 4) {
      			parent.setBackground(Color.BLUE);
      			parent.setForeground(Color.WHITE);
      		} else if (row >= 4 && row < 6) {
      			parent.setBackground(Color.orange);
      			parent.setForeground(Color.BLACK);
      		} else if (row >= 17 && row < 20) {
      			parent.setBackground(Color.RED);
      			parent.setForeground(Color.WHITE);
      		} else {
      			parent.setForeground(Color.black);
      			parent.setBackground(Color.white);
      		}
      		return parent;

      【讨论】:

        【解决方案3】:

        else 只覆盖最后一个if

        另外,不确定您是要在 else 块中设置前景还是背景。

        我假设你想要elses 喜欢:

            if (row >= 0 && row <4) {
                parent.setBackground(Color.BLUE);
                parent.setForeground(Color.WHITE);
            } else if (row >= 4 && row <6) {
                parent.setBackground(Color.orange);
                parent.setForeground(Color.WHITE);
            } else if (row >= 17 && row <19) {
                parent.setBackground(Color.RED);
                parent.setForeground(Color.WHITE);
            } else {
                // setForeground?? - currently whatever was set last time through.
                parent.setBackground(Color.white);
            }
        

        此外,字体通常保留上次设置的任何内容。

        最好写成这样的:

        导入静态 java.awt.Color.*;

            Component component = super.getTableCellRendererComponent(
                table, value, isSelected, hasFocus, row, column
            );
        
            // Here we just set a certain cell to bold
            component.setFont(parent.getFont().deriveFont(
                value.equals(nombre) ? Font.BOLD: Font.PLAIN
            )); 
        
            final Color fg;
            final Color bg;
            if (0 <= row && row < 4) {
                fg = WHITE; bg = BLUE;
            } else if (row <= 4 && row < 6) {
                fg = WHITE; bg = ORANGE;
            } else if (17 <= row && row < 19) {
                fg = WHITE; bg = RED;
            } else {
                fg = BLACK; bg = WHITE;
            }
            component.setForeground(fg);
            component.setBackground(bg);
        

        (注意,thiscomponent 一样有效,但您确实需要进行设置。)

        【讨论】:

          猜你喜欢
          • 2014-06-23
          • 1970-01-01
          • 1970-01-01
          • 2019-05-09
          • 2011-04-02
          • 2011-10-14
          • 2014-11-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多