gpt4 book ai didi

Java - Jtable 颜色

转载 作者:行者123 更新时间:2023-12-02 08:30:22 28 4
gpt4 key购买 nike

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

我正在与上面的代码作斗争。您能否向我解释一下哪里出了问题以及如何解决线条没有改变颜色的问题。

最佳答案

您的代码有两个问题:

  1. 您为表的列设置了一个新的 DefaultTableCellRenderer而不是 this。请注意,您的 Fenetre 类是最重要的getTableCellRendererComponent(...) 方法以更改单元格背景颜色。

  2. 您的 getTableCellRendererComponent(...) 实现委托(delegate)为 tcr 类成员创建渲染器组件的工作,它是 TableCellRenderer 并且未初始化。所以你会得到一个NullPointerException

如何解决?

只需提供正确的TableCellRenderer 实现即可。我不会让类直接实现接口(interface),我会摆脱 tcr 类成员,我会提供一个 DefaultTableCellRenderer 覆盖 getTableCellRendererComponent(.. .) 像这样:

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

关于Java - Jtable 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27545702/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com