上面标有绿色复选标记的代码是正确的,但只是设置了非标题单元格的属性。请注意,TableColumn 类方法 setCellRenderer() 和 setHeaderRenderer() 之间存在差异。
具有多种颜色的标题单元格和非标题黄色单元格的结果表
/********************* FULL WORKING IMPLEMENTATION. *************************
-Table with custom header (multiple colors) and custom non-header cells.
-Cells' manipulated properties are colors, font, opacity and border types.*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class CustomHeaderTable extends JPanel
{
CustomHeaderTable()
{
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Setting header cells
CustomCellRenderer customHeaderCellRenderer =
new CustomCellRenderer(Color.white,
Color.red,
new Font("Consolas",Font.BOLD,14),
BorderFactory.createEtchedBorder(),
true);
for(int columnIndex = 0; columnIndex < table.getTableHeader().getColumnModel().getColumnCount(); columnIndex++)
{
TableColumn tc = table.getTableHeader().getColumnModel().getColumn(columnIndex);
tc.setHeaderRenderer(customHeaderCellRenderer);
}
//Setting non-heather's cells colors, font, borders, and opacity
CustomCellRenderer customNonHeaderCellRenderer =
new CustomCellRenderer(Color.blue,
Color.yellow,
new Font("Courier",Font.PLAIN,12),
BorderFactory.createLineBorder(Color.gray),
true);
for(int columnIndex = 0; columnIndex < table.getTableHeader().getColumnModel().getColumnCount(); columnIndex++)
{
TableColumn tc = table.getTableHeader().getColumnModel().getColumn(columnIndex);
tc.setCellRenderer(customNonHeaderCellRenderer);
}
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
}
public class CustomCellRenderer extends JLabel implements TableCellRenderer
{
public CustomCellRenderer(Color foregroundColor, Color backgroundColor, Font font, Border border, boolean isOpaque)
{
setFont(font);
setOpaque(isOpaque);
setForeground(foregroundColor);
setBackground(backgroundColor);
setBorder(border);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
setText(value.toString());
return this;
}
}
class MyTableModel extends AbstractTableModel
{
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",`enter code here`
"Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), false},
{"John", "Doe",
"Rowing", new Integer(3), true},
{"Sue", "Black",
"Knitting", new Integer(2), false},
{"Jane", "White",
"Speed reading", new Integer(20), true},
{"Joe", "Brown",
"Pool", new Integer(10), false}
};
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return data[row][col];
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col)
{
if (col < 3)
{
return false;
}
else
{
return true;
}
}
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("CustomHeaderTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new CustomHeaderTable();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}