单元格颜色设置无效
package com.corejava.L613;/**
* 例题6-13颜色设置未完成!
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableCellRenderTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new TableCellRenderFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class TableCellRenderFrame extends JFrame {
public TableCellRenderFrame() {
setTitle("表格组合测试");
setSize(600, 400);
TableModel model = new PlanetTableModel();
JTable table = new JTable(model);
table.setRowSelectionAllowed(false); // 设置是否可以选择此模型中的行。
//建立单元格渲染器与编辑器
table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
table.setDefaultEditor(Color.class, new ColorTableCellEditor());
JComboBox moonCombo = new JComboBox();
for (int i = 0; i <= 20; i++)
moonCombo.addItem(i);
TableColumnModel columnModel = table.getColumnModel();
TableColumn moonColumn = columnModel
.getColumn(PlanetTableModel.MOONS_COLUMN);
moonColumn.setCellEditor(new DefaultCellEditor(moonCombo));
moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
moonColumn.setHeaderValue(new ImageIcon("Moons.gif"));
// 显示表格
table.setRowHeight(100);
add(new JScrollPane(table), BorderLayout.CENTER);
}
}
class PlanetTableModel extends AbstractTableModel {
public static final int PLANET_COLUMN = 0;
public static final int MOONS_COLUMN = 2;
public static final int GASEOUS_COLUMN = 3;
public static final int COLOR_COLUMN = 4;
public String getColumnName(int c) {
return columnNames[c];
}
public Class getColumnClass(int c) {
return cells[0][c].getClass();
}
public int getColumnCount() {
return cells[0].length;
}
public int getRowCount() {
return cells.length;
}
public Object getValueAt(int r, int c) {
return cells[r][c];
}
public void setValueAt(Object obj, int r, int c) {
cells[r][c] = obj;
}
public boolean isCellEditable(int r, int c) {
return c == PLANET_COLUMN || c == MOONS_COLUMN || c == GASEOUS_COLUMN
|| c == COLOR_COLUMN;
}
private Object[][] cells = {
{ "Mercury", 2440.0, 0, false, Color.yellow, new ImageIcon("a.gif") },
{ "Venus", 6052.0, 0, false, Color.yellow, new ImageIcon("a.gif") },
{ "Earth",6378.0, 1, false, Color.blue, new ImageIcon("a.gif") },
{ "Mars", 3397.0, 2, false, Color.red, new ImageIcon("a.gif") },
{ "Jupiter", 71492.0, 16, true, Color.orange, new ImageIcon("a.gif") },
{ "Saturn", 50268.0, 18, true, Color.orange, new ImageIcon("a.gif") },
{ "Uranus", 25559.0, 17, true, Color.blue, new ImageIcon("a.gif") },
{ "Neptune", 24766.0, 8, true, Color.blue, new ImageIcon("a.gif") },
{ "Pluto", 1137.0, 1, false, Color.black, new ImageIcon("a.gif") }};
private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous",
"Color", "Image" };
}
class ColorTableCellRenderer extends JPanel implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setBackground((Color) value);
if (hasFocus)
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
else
setBorder(null);
return this;
}
}
class ColorTableCellEditor extends AbstractCellEditor implements
TableCellEditor {
private Color color;
private JColorChooser colorChooser;
private JDialog colorDialog;
private JPanel panel;
public ColorTableCellEditor() {
panel = new JPanel();
colorChooser = new JColorChooser();
colorDialog = JColorChooser.createDialog(null, "色彩选择", false,
colorChooser, new ActionListener() {
public void actionPerformed(ActionEvent event) {
stopCellEditing();
}
}, new ActionListener() {
public void actionPerformed(ActionEvent event) {
cancelCellEditing();
}
});
colorDialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
cancelCellEditing();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelecte, int row, int column) {
colorChooser.setColor((Color) value);
return panel;
}
public boolean shouldSelectCell(EventObject anEvent) {
colorDialog.setVisible(true);
return true;
}
public void cancelCellEditing() {
colorDialog.setVisible(false);
super.cancelCellEditing();
}
public boolean stopCellEditing() {
colorDialog.setVisible(false);
super.cancelCellEditing();
return true;
}
public Object getCellEditorValue() {
return colorChooser.getColor();
}
}
这段程序在Color列单击时会弹出一个颜色选择对话框,但是选择后,单元格颜色没有改变,为何?