セルレンダラーを正しく使用していません。現在、あなたのコードは、JTableのモデルを塗りつぶした後、レンダラー内でそのリストを反復するときに、色でArrayList、desiredColorsを塗りつぶしますが、すべてのセルには単に最後の色のカラーが与えられます配列リスト。
代わりに、レンダラーにコンストラクタとレンダラーのgetTableCellRendererComponent
メソッドの2つの色を与え、value
パラメータで取得したテーブルのデータをテストし、その情報を使用してレンダラーの色例えば
、例えば
class MyRenderer extends DefaultTableCellRenderer {
private Color chefColor;
private Color defaultColor;
public MyRenderer(Color chefColor, Color defaultColor) {
this.chefColor = chefColor;
this.defaultColor = defaultColor;
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component superComp = super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
// here test if row holds a chef item
// if so, set background color to chefColor
// otherwise set background color to the defaultColor.
return superComp;
}
}
:
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
/**
* Example of using a DefaultTableModel that holds a row of specific type
* Example of hidden column determining color of cells
* @author Pete
*
*/
@SuppressWarnings("serial")
public class SimpleTableExampe extends JPanel {
RestaurantEmployee[] employees = {
new RestaurantEmployee("John", true, 1),
new RestaurantEmployee("Bill", false, 2),
new RestaurantEmployee("Sally", true, 3),
new RestaurantEmployee("Frank", false, 4),
new RestaurantEmployee("Jim", false, 5),
new RestaurantEmployee("Imelda", true, 6)
};
private MyTableModel tableModel = new MyTableModel();
private JTable table = new JTable(tableModel);
public SimpleTableExampe() {
MyRenderer renderer = new MyRenderer(Color.RED, Color.GREEN);
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(renderer);
}
add(new JScrollPane(table));
for (RestaurantEmployee employee : employees) {
tableModel.addEmployee(employee);
}
}
private static void createAndShowGui() {
SimpleTableExampe mainPanel = new SimpleTableExampe();
JFrame frame = new JFrame("SimpleTableExampe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class RestaurantEmployee {
private String name;
private boolean chef;
private int id;
public RestaurantEmployee(String name, boolean chef, int id) {
this.name = name;
this.chef = chef;
this.id = id;
}
public String getName() {
return name;
}
public boolean isChef() {
return chef;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "RestaurantEmployee [name=" + name + ", chef=" + chef + ", id=" + id + "]";
}
}
@SuppressWarnings("serial")
class MyTableModel extends DefaultTableModel {
private static final String[] COLUMN_NAMES = { "Name", "ID" };
public MyTableModel() {
super(COLUMN_NAMES, 0);
}
public void addEmployee(RestaurantEmployee employee) {
addRow(new RestaurantEmployee[] { employee });
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (getRowCount() > 0) {
return getValueAt(0, columnIndex).getClass();
} else {
return super.getColumnClass(columnIndex);
}
}
public RestaurantEmployee getEmployee(int row) {
return (RestaurantEmployee) super.getValueAt(row, 0);
}
@Override
public Object getValueAt(int row, int column) {
RestaurantEmployee employee = (RestaurantEmployee) super.getValueAt(row, 0);
switch (column) {
case 0:
return employee.getName();
case 1:
return employee.getId();
default:
return null;
}
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}
@SuppressWarnings("serial")
class MyRenderer extends DefaultTableCellRenderer {
private static final int ALPHA = 60;
private Color chefColor;
private Color defaultColor;
public MyRenderer(Color chefColor, Color defaultColor) {
this.chefColor = chefColor;
this.defaultColor = defaultColor;
setOpaque(true);
setHorizontalAlignment(SwingConstants.CENTER);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component superComp = super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
if (value != null) {
MyTableModel tableModel = (MyTableModel) table.getModel();
boolean chef = tableModel.getEmployee(row).isChef();
Color c = chef ? chefColor : defaultColor;
if (!hasFocus && !isSelected) {
c = new Color(c.getRed(), c.getGreen(), c.getBlue(), ALPHA);
}
superComp.setBackground(c);
}
return superComp;
}
}
あなたはそれがレンダラーの仕組みではありません、テーブルモデルからあなたの色を設定し、実際に何をやっていることは、後方でいます。代わりに、レンダラークラスはデータの状態をテストし、その情報をレンダラークラス自体から使用する必要があります。 –
なので、レンダラー内のforループは意味をなさない - それを取り除く。代わりに、行のデータをテストし、それを使用して色を設定します。 –