getTableCellRendererComponent

getTableCellRendererComponent

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class Fenetre extends JFrame implements TableCellRenderer {
    private static TableCellRenderer tcr;

    public Fenetre(TableCellRenderer tcr){
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Liste des PC de la DTARS");
        this.setSize(500, 250);

        Object[][] data = {
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","A REMPLACER","24"},
                {"nomPc", "PC Fixe", "xxxx","A REMPLACER","24"},
                {"nomPc", "PC Portable", "xxxx","OK","NA"},
                {"nomPc", "PC Portable", "xxxx","OK","NA"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},
                {"nomPc", "PC Fixe", "xxxx","OK","24"},

        };

         String  title[] = {"Nom Machine", "Type", "Utilisateur","Etat","Ecran"};
        JTable tableau = new JTable(data, title);
        //tableau.setBackground(Color.cyan);
        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        tableau.getColumn("Type").setCellRenderer( rightRenderer );
        tableau.getColumn("Utilisateur").setCellRenderer( rightRenderer );
        tableau.getColumn("Etat").setCellRenderer( rightRenderer );
        tableau.getColumn("Ecran").setCellRenderer( rightRenderer );
        tableau.setAutoCreateRowSorter(true);
        this.getContentPane().add(new JScrollPane(tableau));

    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component comp = this.tcr.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if ((row & 1) == 0) { // Ligne paire
            comp.setBackground(Color.RED);
        } else { // Ligne impaire
            comp.setBackground(Color.BLUE);
        }
        return comp;
    }

    public static void main(String[] args){
        Fenetre fen = new Fenetre(tcr);
        fen.setVisible(true);
    }
}

我正在与上面的代码战斗。您能告诉我什么地方不对以及如何解决线条不变色的问题。

最佳答案

您的代码有两个问题:

  • 您在表的列中设置了一个新的DefaultTableCellRenderer而不是this。请注意,您的Fenetre类是覆盖的一个getTableCellRendererComponent(...)方法以更改单元格
    背景颜色。
  • 您的getTableCellRendererComponent(...)实现延迟
    创建tcr类成员的渲染器组件的工作,
    TableCellRenderer且未初始化。所以你会得到一个NullPointerException

  • 怎么解决呢?

    只需提供正确的TableCellRenderer实现即可。我不会让该类直接实现接口(interface),而是要摆脱tcr类成员,并提供一个覆盖DefaultTableCellRenderergetTableCellRendererComponent(...),如下所示:
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if ((row % 2) == 0) { // Ligne paire
                comp.setBackground(Color.RED);
            } else { // Ligne impaire
                comp.setBackground(Color.BLUE);
            }
            return comp;
        }
    };
    
    renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
    
    tableau.getColumn("Type").setCellRenderer(renderer);
    tableau.getColumn("Utilisateur").setCellRenderer(renderer);
    tableau.getColumn("Etat").setCellRenderer(renderer);
    tableau.getColumn("Ecran").setCellRenderer(renderer);
    

    07-26 05:23