4

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

我希望有人知道这个问题的解决方案,或者可以给我一个想法,我在哪里迷失了一些东西。

我正在使用这个循环

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

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

1 回答 1

8

Do not update table data in your CutomRenderer class.Renderer的班级应该检查条件并为单元格着色。我已经使用了您的CustomRenderer课程并根据单元格中存在的数据渲染了单元格。如果单元格的数据为“Y”,则将其着色为黄色。如果数据为“N”,则将其着色为灰色。

在 JTable 中渲染

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;
    }
}
于 2013-01-28T15:42:43.303 回答