在 JTable 中设置不同颜色的行?

Setting rows of different colours in JTable?

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

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

else只覆盖最后if.

此外,不确定您是否想在 else 块中设置前景或背景。

我假设你想要 else 像:

    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 一样有效,但您需要对其进行设置。)

还有 "else" 块剩余。这是代码。

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;

另一种选择是覆盖 JTableprepareRenderer(...) 方法。即使您 table 在您的模型中有字符串、整数、对象,这种方法仍然有效。查看 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(...) 方法获取值以确定是否要将单元格设为粗体。