jtable cellrenderer在运行时更改单元格的背景颜色 [英] jtable cellrenderer changes backgroundcolor of cells while running

查看:161
本文介绍了jtable cellrenderer在运行时更改单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表格和颜色特定的单元格,黄色,红色或白色,具体取决于其他列的内容。为此,我循环填充值的行,然后检查内容。对于当前显示在屏幕上的每一行都可以正常工作,但是当程序到达未显示的行时,或者如果用户尝试滚动每个单元格,则将其背景颜色更改为白色。我在网上搜索了解决方案,唯一合情合理的想法是在每次循环后重置cellRenderer,这不起作用,因为它也会重置每个单元格。

I'm trying to create a table and color specific cells either yellow, red or white, depending on the content of other columns. For that I am looping through the rows filling in the values and then checking on the contents. that works just fine for every row that is currently displayed on the screen, however when the program reaches rows that are not displayed or if the user try's to scroll every cell changes its backgroundcolor back to white. I have searched the web for solutions and the only idea that sounded reasonable was to reset the cellRenderer after each loop, which does not work because it resets every cell too.

I希望有人知道这方面的解决方案,或者可以让我知道我在哪里迷雾。

I hope someone knows a solution for this or can give me an idea where i mist out on something.

我正在使用这个循环

for(int e = 0; e < modules.size(); e++)
    {
    gui.clearOutputStream();
    gui.getOutputStream().setText("Load Modul " + modules.get(e) + "\r\n");
    version = getVersion(modules.get(e));

    //Update current Row
    updateRow(gui.getReleaseTabelle(), e);
    }

调用此方法

public void updateRow(JTable target, int row){
//...
//insert Values here
//...
CustomRenderer cr = new CustomRenderer();
        cr.tab = target;
        if(!target.getValueAt(row, 2).equals(target.getValueAt(row, 3)))
        {
            cr.Val1 = target.getValueAt(row, 1).toString();
            target.setValueAt("X", row, 1);
        }
        else if(!target.getValueAt(row, 7).equals(""))
        {
            cr.Val1 = target.getValueAt(row, 1).toString();
            target.setValueAt("Y", row, 1);
        }
        else
        {

        }
        target.getColumnModel().getColumn(1).setCellRenderer(cr);

}

这是我的CustomRenderer

and this is my CustomRenderer

class CustomRenderer extends DefaultTableCellRenderer 
    {
private static final long serialVersionUID = 6703872492730589499L;
        public String Val1; 
        public JTable tab;

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if(tab.getValueAt(row, 1).equals("Y")){
                cell.setBackground(new java.awt.Color(255, 255, 0));
                tab.setValueAt(Val1, row, 1);
            }
            else if(tab.getValueAt(row, 1).equals("X")){
                cell.setBackground(new java.awt.Color(255, 50, 50));
                tab.setValueAt(Val1, row, 1);
            }
            else
            {
                //do nothing
            }
            return cell;
        }
    }


推荐答案

不要更新CutomRenderer类中的表数据。渲染器类应检查条件并为单元格着色。我使用了 CustomRenderer 类,并根据单元格中的数据渲染单元格。如果单元格的数据为Y,则将其变为黄色。如果数据为N,则将其颜色设置为灰色。

Do not update table data in your CutomRenderer class. You Renderer class should check the condition and color the cells. I have used your CustomRenderer class and rendered the cells basing on the data present in the cells. If the data of the cell is 'Y' color it to Yellow. If the data is 'N' then color it to Grey.

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


public class ColoringCells {

    private static Object[] columnName = {"Yes", "No"};
    private static Object[][] data = {
            {"Y", "N"},
            {"N", "Y"},
            {"Y", "N"}
    };


    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {

                JFrame frame = new JFrame();
                JTable table = new JTable(data, columnName);
                table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());
                table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer());

                frame.add(new JScrollPane(table));
                frame.setTitle("Rendering in JTable");
                frame.pack();
                frame.setVisible(true);
            }
        };

        EventQueue.invokeLater(r);
    }
}


class CustomRenderer extends DefaultTableCellRenderer 
{
private static final long serialVersionUID = 6703872492730589499L;

    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("Y")){
            cellComponent.setBackground(Color.YELLOW);
        } else if(table.getValueAt(row, column).equals("N")){
            cellComponent.setBackground(Color.GRAY);
        }
        return cellComponent;
    }
}

这篇关于jtable cellrenderer在运行时更改单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆