2

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Daniel Pereira
  • 435
  • 2
  • 7
  • 13

3 Answers3

6

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 ));
6

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

JTableHeader header = table.getTableHeader();
header.setFont( header.getFont().deriveFont(16) );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • There is a bug here. You would need to pass "16f" as the parameter, not just 16. The deriveFont(int) method is a different method than deriveFont(float). – Michael Apr 01 '13 at 21:50
1

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;
        }
    });
mKorbel
  • 109,525
  • 20
  • 134
  • 319