void setTableHeaderColor(JTable table, int columnIndex, Color c) { TableColumn column = table.getTableHeader().getColumnModel().getColumn(columnIndex); DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer(); cellRenderer.setBackground(c); column.setHeaderRenderer(cellRenderer); }
public static void main(String[] args){ TestFrame mt = new TestFrame(); mt.setLocationRelativeTo(null); mt.setVisible(true); } } 运行效果:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class TestJTable extends JFrame{
public TestJTable() {
this.setLayout(new BorderLayout());
this.setSize(new Dimension(400,300));
JTable table = new JTable(15,5);
this.add(table, BorderLayout.NORTH);
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if( column==1){
cell.setBackground(Color.RED);
} else if ( column==3) {
cell.setBackground(Color.BLUE);
} else {
cell.setBackground(table.getBackground());
}
return cell;
}
});
this.setVisible(true);
}
public static void main(String[] args){
TestJTable mt = new TestJTable();
}}
改了。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
public class TestJTable extends JFrame{
public TestJTable() {
this.setLayout(new BorderLayout());
this.setSize(new Dimension(400,300));
JTable table = new JTable(15,5);
JTableHeader header = table.getTableHeader();
this.add(header, BorderLayout.NORTH);
this.add(table, BorderLayout.SOUTH);
header.setDefaultRenderer(new DefaultTableCellRenderer(){
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if( column==1){
cell.setBackground(Color.RED);
} else if ( column==3) {
cell.setBackground(Color.BLUE);
} else {
cell.setBackground(table.getBackground());
}
return cell;
}
});
this.setVisible(true);
}
public static void main(String[] args){
TestJTable mt = new TestJTable();
}}
1、把设置表头特定列颜色的代码放在一个方法中,使用起来比较方便。
2、没有孤立表头,避免了界面的难看。代码内容:package test;import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;public class TestFrame extends JFrame{
public TestFrame() {
super("表头颜色设置");
this.setSize(new Dimension(400,300));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(4,3);
this.add(new JScrollPane(table)); setTableHeaderColor(table, 0, Color.RED); //把表头的第1列设为红色
setTableHeaderColor(table, 1, Color.YELLOW); //把表头的第2列设为黄色
setTableHeaderColor(table, 2, Color.GREEN); //把表头的第3列设为绿色
}
void setTableHeaderColor(JTable table, int columnIndex, final Color c)
{
TableColumn column = table.getTableHeader().getColumnModel().getColumn(columnIndex);
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer(){
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JComponent comp = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
comp.setBackground(c);
comp.setBorder(BorderFactory.createRaisedBevelBorder());
return comp;
}
};
column.setHeaderRenderer(cellRenderer);
}
public static void main(String[] args){
TestFrame mt = new TestFrame();
mt.setLocationRelativeTo(null);
mt.setVisible(true);
}
}
运行效果:
import javax.swing.*;
import javax.swing.table.*;public class TestFrame extends JFrame{
public TestFrame() {
super("表头颜色设置");
this.setSize(new Dimension(400,300));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JTable table = new JTable(4,3);
this.add(new JScrollPane(table)); setTableHeaderColor(table, 0, Color.RED); //把表头的第1列设为红色
setTableHeaderColor(table, 1, Color.YELLOW); //把表头的第2列设为黄色
setTableHeaderColor(table, 2, Color.GREEN); //把表头的第3列设为绿色
}
void setTableHeaderColor(JTable table, int columnIndex, Color c)
{
TableColumn column = table.getTableHeader().getColumnModel().getColumn(columnIndex);
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
cellRenderer.setBackground(c);
column.setHeaderRenderer(cellRenderer);
}
public static void main(String[] args){
TestFrame mt = new TestFrame();
mt.setLocationRelativeTo(null);
mt.setVisible(true);
}
}
运行效果: