How can I display table headers using the table / model object?

final JTable table = new JTable(data,colNames);

    1 answer 1

    Code

      Enumeration<TableColumn> columns = table.getColumnModel().getColumns(); while ( columns.hasMoreElements() ) { TableColumn column = columns.nextElement(); System.out.println( column.getHeaderValue() ); } 

    or

      TableColumnModel columnModel = table.getColumnModel(); for ( int index = 0; index < columnModel.getColumnCount(); index += 1 ) { TableColumn column = columnModel.getColumn( index ); System.out.println( column.getHeaderValue() ); } 

    will display the headings (text above the columns) of the visible columns in the display order in the table.

      for ( int index = 0; index < table.getColumnCount(); index += 1 ) { System.out.println( table.getColumnName( index ) ); } 

    will display the names (what the TableModel.getColumnName( int index ) returns in the display order).