1

i want to ask how to change the style(font, background color) of column name in table. i'm using Jtable, Jframe, Java

table_test.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "NO", "CODE", "ADDRESS", "CLASS"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false, false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    table_test.setRowHeight(30);
    table_test.setSelectionMode(0);

    jScrollPane1.setViewportView(table_test);
    table_test.getColumnModel().getColumn(0).setMinWidth(55);
    table_test.getColumnModel().getColumn(0).setMaxWidth(55);
    table_test.getColumnModel().getColumn(1).setMinWidth(120);
    table_test.getColumnModel().getColumn(1).setMaxWidth(120);
    table_test.getColumnModel().getColumn(2).setMinWidth(280);
    table_test.getColumnModel().getColumn(2).setMaxWidth(280);
    table_test.getColumnModel().getColumn(3).setMinWidth(63);
    table_test.getColumnModel().getColumn(3).setMaxWidth(63);

This is the image : enter image description here

dtnder
  • 381
  • 2
  • 10
  • 25

1 Answers1

2

You can set it like using table header set font,

table.getTableHeader().setFont( ... );

Example:

table.getTableHeader().setFont(new Font("Times New Roman", Font.BOLD, 12));
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • 1
    Also consider altering the UI defaults, as suggested [here](http://stackoverflow.com/a/6222720/230513). – trashgod Nov 14 '12 at 14:48