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

时间:2018-12-21 21:43:37

标签: java swing colors jtable

下午好, 我的用于渲染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;
    }

3 个答案:

答案 0 :(得分:0)

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一样好,但是您需要进行设置。)

答案 1 :(得分:0)

还有“ 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;

答案 2 :(得分:0)

另一种选择是覆盖prepareRenderer(...)的{​​{1}}方法。即使您的表在模型中具有String,Integer,对象,该方法也将起作用。请查看Table Row Rendering,以了解更多信息。

此建议的基本实现如下:

JTable

您需要使用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; } }; 方法来获取该值,以确定是否要使单元格变为粗体。

相关问题