siper camm
siper camm

Reputation: 77

how to customize jTable header column font size in Netbeans?

I tried to change jtable header font size in Netbeans. but couldn't yet. anyway the table rows font size is changed successfully.

Here is the way I used:

?

The output after changes:

?

Problem : The Header font size is not changed. but I want to change that also. so pls help me how to do.

Upvotes: 2

Views: 3510

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

One way would be to use the UIManager and replace the default Font with the one you want

Font font = UIManager.getFont("TableHeader.font");
font = font.deriveFont(48f);
UIManager.put("TableHeader.font", font);

Which will replace the font used by all the tables in the system

Column Headers

Another way is to provide a custom TableCellRenderer for the columns you want to change, it's a little more work, but provides more flexibility as you can decide where you want to apply them. You could wrap this inside your own custom JTableHeader, but I'm just providing some basic ideas.

public 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;
    }

}

Which is installed using something like...

HeaderRenderer header = new HeaderRenderer(table.getTableHeader().getDefaultRenderer());
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setHeaderRenderer(header);

And produces something like...

Custom Column Header

Credit to Kleopatra for this idea

The long and short of it is, you're going to have to get your hands dirty and write some code, the form editor won't do everything for you

Upvotes: 5

Related Questions