Java JTable Header tableHeaders(JTable jtable)

Here you can find the source of tableHeaders(JTable jtable)

Description

Convert the table model column names to an array of strings.

License

Open Source License

Parameter

Parameter Description
jtable table to get column names from

Return

array of strings with column names

Declaration

public static String[] tableHeaders(JTable jtable) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JTable;

import javax.swing.table.TableModel;

public class Main {
    /**/*from w ww . j a  v a2s  .  c o m*/
     * Convert the table model column names to an array of strings.
     * @param jtable table to get column names from
     * @return array of strings with column names
     */
    public static String[] tableHeaders(JTable jtable) {
        TableModel tm = jtable.getModel();
        List<String> colNameList = new ArrayList<>();
        for (int i = 0; i < tm.getColumnCount(); i++) {
            colNameList.add(tm.getColumnName(i));
        }
        return colNameList.toArray(new String[colNameList.size()]);
    }

    /**
     * Convert the table model column names to an array of strings.
     * @param jtable table to get column names from
     * @return array of strings with column names
     */
    public static String[] tableHeaders(JTable jtable, Iterable<Integer> columnIndexes) {
        TableModel tm = jtable.getModel();
        List<String> colNameList = new ArrayList<>();
        for (Integer colIndex : columnIndexes) {
            if (colIndex == null) {
                throw new IllegalArgumentException("columnIndexe contains null : " + columnIndexes);
            }
            int i = (int) colIndex;
            if (i < 0 || i > tm.getColumnCount()) {
                throw new IllegalArgumentException("columnIndexes contains " + i + " outside range 0 to "
                        + tm.getColumnCount() + " : " + columnIndexes);
            }
            colNameList.add(tm.getColumnName(i));
        }
        return colNameList.toArray(new String[colNameList.size()]);
    }
}

Related

  1. setOptimalHeaderWidth(int col)
  2. setPressedColumn(JTableHeader tableHeader, int columnModelIndex)
  3. setTableHeaderCellRenderer(TableColumn tableCol)
  4. setupAsRowHeader(final JTable table)
  5. sizeColumnsToFit(JTable table, boolean useHeader, boolean useContent)