问题
I want to make the header of a jtable with defaulttablemodel BOLD. Here's my code
class TablePanel extends JPanel {
private String[] COLUMNS = {"FAMILY MEMBERS", "STAR (NAKSHATRA)"};
private DefaultTableModel model = new DefaultTableModel(COLUMNS, 0){
public boolean isCellEditable(int row, int column) {
return false;
}
};
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(){
public void setHorizontalAlignment(int alignment) {
alignment = (int) CENTER_ALIGNMENT;
};
};
public JTable table = new JTable(model){
public void setDefaultRenderer(java.lang.Class<?> columnClass, TableCellRenderer renderer) {
columnClass= String.class;
renderer=centerRenderer;
};
public void setBackground(Color bg) {
bg = Color.yellow;
};
};
I want to make the column headers BOLD. any hint on this is appreciated
回答1:
Strangely enough, a table column header uses the same concept to renderer as the table does...
DefaultTableCellRenderer header = new DefaultTableCellRenderer();
header.setFont(header.getFont().deriveFont(Font.BOLD));
JTable table = new JTable();
TableColumnModel model = table.getColumnModel();
model.getColumn(0).setHeaderRenderer(header);
For example. But I don't think it will turn out the way you want it to...
Instead, you could try something like...
DefaultTableModel model = new DefaultTableModel(new Object[]{"<html><b>Happy</b></html>", "Sad"}, 0);
JTable table = new JTable(model);
回答2:
A cleaner (than @Mad's) strategy for custom visual in a renderer is to implement a wrapping renderer around the default: it delegates all configuration to the default and than applies custom decorations. Doing so keeps the LAF specific visuals.
public static class HeaderRenderer implements UIResource, TableCellRenderer {
private TableCellRenderer original;
public HeaderRenderer(TableCellRenderer original) {
this.original = original;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component comp = original.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
comp.setFont(comp.getFont().deriveFont(Font.BOLD));
return comp;
}
}
To use, replace the header's default with the wrapping renderer and update on changes of LAF:
// replace the inital default with our wrapper
TableCellRenderer original = table.getTableHeader().getDefaultRenderer();
table.getTableHeader().setDefaultRenderer(new HeaderRenderer(original));
// a propertyChangeListener that updates the wrapper
PropertyChangeListener pl = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// cleanup (to be on the safe side)
if (table.getTableHeader().getDefaultRenderer() instanceof HeaderRenderer) {
HeaderRenderer renderer = (HeaderRenderer) table.getTableHeader().getDefaultRenderer();
table.getTableHeader().setDefaultRenderer(renderer.original);
}
// invoke to make sure the ui delegate updated the header renderer
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TableCellRenderer r = table.getTableHeader().getDefaultRenderer();
if (!(r instanceof HeaderRenderer)) {
table.getTableHeader().setDefaultRenderer(new HeaderRenderer(r));
table.getTableHeader().repaint();
}
}
});
}
};
// register the listener with the UIManager to get notified on LAF changes
UIManager.addPropertyChangeListener(pl);
来源:https://stackoverflow.com/questions/19175997/how-to-make-the-defaulttablemodel-table-header-bold