Here you can find the source of tableHeaders(JTable jtable)
Parameter | Description |
---|---|
jtable | table to get column names from |
public static String[] tableHeaders(JTable jtable)
//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()]); } }