|
// MyTableRenderer.java
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class MyTableRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 6940369712189637052L;
private int rows = 0;
private int cols = 0;
private Color[][] fg = null;
private Color[][] bg = null;
private Color defaultFg = Color.BLACK;
private Color defaultBg = Color.WHITE;
private JTable table = null;
public MyTableRenderer(JTable table) {
if (table == null) return;
this.table = table;
table.setDefaultRenderer(Object.class, this);
defaultFg = table.getForeground();
defaultBg = table.getBackground();
rows = table.getRowCount();
cols = table.getColumnCount();
if (rows > 0 && cols > 0) {
fg = new Color[rows][cols];
bg = new Color[rows][cols];
}
}
public void setCellBgColor(int row, int col, Color color) {
if (row < 0 || col < 0) return;
if (row >= rows || col >= cols) return;
bg[row][col] = color;
if (table != null) table.repaint();
}
public void setCellFgColor(int row, int col, Color color) {
if (row < 0 || col < 0) return;
if (row >= rows || col >= cols) return;
fg[row][col] = color;
if (table != null) table.repaint();
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
setForeground(defaultFg);
setBackground(defaultBg);
int realCol = table.convertColumnIndexToModel(column);
if (fg != null && fg[row][realCol] != null) {
setForeground(fg[row][realCol]);
}
if (bg != null && bg[row][realCol] != null) {
setBackground(bg[row][realCol]);
}
return this;
}
}
/*** 用法示例 ***/
// JTableDemo.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableDemo implements ActionListener {
private MyTableRenderer renderer = null;
Color[] colors = {
Color.BLUE, Color.CYAN, Color.DARK_GRAY,
Color.GREEN, Color.RED, Color.YELLOW
};
int currentColor = 0;
@Override
public void actionPerformed(ActionEvent e) {
renderer.setCellBgColor(0, 0, colors[currentColor]);
renderer.setCellFgColor(1, 1, colors[currentColor]);
currentColor = (currentColor+1) % colors.length;
}
public void Demo() {
Object[] title = { "Title1", "Title2", "Title3" };
Object[][] data = { {10, 11, 12}, {20, 21, 22} };
DefaultTableModel model = new DefaultTableModel(data, title);
JTable table = new JTable(model);
renderer = new MyTableRenderer(table);
renderer.setCellBgColor(0, 2, Color.PINK);
JButton button = new JButton("Change");
button.addActionListener(this);
button.setPreferredSize(new Dimension(120,30));
JScrollPane pnl2 = new JScrollPane(table);
JPanel pnl3 = new JPanel();
pnl3.add(button);
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(pnl2, BorderLayout.CENTER);
pnl.add(pnl3, BorderLayout.SOUTH);
JFrame wnd = new JFrame("JTable Demo");
wnd.setPreferredSize(new Dimension(300,200));
wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.getContentPane().add(pnl);
wnd.pack();
wnd.setVisible(true);
}
public static void main(String[] args) {
JTableDemo me = new JTableDemo();
me.Demo();
}
} |
|