How to increase the Font of the title column in JTable Swing?

10,350

Solution 1

You just need to call the getTableHeader() method. Then on the object of class JTableHeader use the setFont(/*font*/) method to set new font.

table.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 15 ));

Solution 2

To keep the same Font family and just change the size you can use:

JTableHeader header = table.getTableHeader();
header.setFont( header.getFont().deriveFont(16) );

Solution 3

not sure from your question, then I post both options

1) set Font for JTable myTable.setFont(new Font("Arial", Font.PLAIN, 10))

2) set Font for TableHeader

    final TableCellRenderer tcrOs = table.getTableHeader().getDefaultRenderer();
    table.getTableHeader().setDefaultRenderer(new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));
            lbl.setHorizontalAlignment(SwingConstants.LEFT);
            if (isSelected) {
                lbl.setForeground(Color.red);
                lbl.setFont(new Font("Arial", Font.BOLD, 12));
            } else {
                lbl.setForeground(Color.darkGray);
                lbl.setFont(new Font("Arial", Font.PLAIN, 10));
            }
            return lbl;
        }
    });
Share:
10,350
Daniel Pereira
Author by

Daniel Pereira

Updated on June 04, 2022

Comments

  • Daniel Pereira
    Daniel Pereira almost 2 years

    I would like to know how to increase the Font size of the title column in JTable Swing?

    I'm usning Netbeans.

    Best regards.

    Daniel