Java Utililty Methods JTable Column

List of utility methods to do JTable Column

Description

The list of methods to do JTable Column are organized into topic(s).

Method

Object[][]createColumnNames(TableModel model)
create Column Names
Object rowValues[][] = new Object[2][0];
rowValues[0] = new Object[model.getColumnCount() != 0 ? model.getColumnCount() - 1 : 0];
rowValues[1] = new Object[model.getColumnCount() != 0 ? 1 : 0];
for (int a = 0; a < model.getColumnCount() - 1; a++)
    rowValues[0][a] = model.getColumnName(a);
if (rowValues[1].length != 0)
    rowValues[1][0] = model.getColumnName(model.getColumnCount() - 1);
return rowValues;
...
DefaultTableModelcreateDefaultTableModel(String[] columnNames)
create Default Table Model
return new DefaultTableModel(new Object[][] {}, columnNames) {
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
        case 1:
            return Boolean.class;
        default:
            return String.class;
...
DefaultTableModelcreateUneditableTableModel(String... columnNames)
Create an uneditable default table model with the given column names
return new DefaultTableModel(columnNames, 0) {
    private static final long serialVersionUID = 6490129562872132785L;
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
};
voidensureColumnCount(int count, JTable table)
Method checks if the column count is greater or equal to specified count, if not - it add a column
if (count <= table.getColumnCount())
    return;
int[] minwidth = new int[count];
int[] maxwidth = new int[count];
int[] prefwidth = new int[count];
int i, j;
for (i = 0; i < table.getColumnCount(); ++i) {
    minwidth[i] = table.getColumnModel().getColumn(i).getMinWidth();
...
voidfilterTable(JTable table, DefaultTableModel dtm, String text, int[] columns, boolean forceRegEx)
filter Table
if (null == table || null == dtm || null == text || text.isEmpty() || null == columns
        || columns.length < 1) {
    return;
if (!forceRegEx) {
    text = text.toLowerCase();
for (int cnt = table.getRowCount() - 1; cnt >= 0; cnt--) {
...
intfindColumn(TableModel m, String columnName)
Finds a column for the given column name in a table model
if (columnName != null) {
    if (m instanceof AbstractTableModel) {
        return ((AbstractTableModel) m).findColumn(columnName);
    } else {
        for (int i = 0; i < m.getColumnCount(); i++) {
            if (columnName.equals(m.getColumnName(i))) {
                return i;
return -1;
intfindColumn(TableModel model, String name)
find Column
for (int i = 0; i < model.getColumnCount(); i++) {
    if (name.equals(model.getColumnName(i))) {
        return i;
return -1;
voidfitColumns(JTable table)
Fit the columns of a JTable regarding it's contents.
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
    int width = COLUMN_WIDTH_MIN;
    for (int row = 0; row < table.getRowCount(); row++) {
        Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
        width = Math.max(comp.getPreferredSize().width + 1, width);
    columnModel.getColumn(column).setPreferredWidth(width);
...
voidFitTableColumns(JTable myTable)
Fit Table Columns
JTableHeader header = myTable.getTableHeader();
int rowCount = myTable.getRowCount();
Enumeration columns = myTable.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
    TableColumn column = (TableColumn) columns.nextElement();
    int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
    int width = (int) myTable.getTableHeader().getDefaultRenderer()
            .getTableCellRendererComponent(myTable, column.getIdentifier(), false, false, -1, col)
...
StringformatStringListToTable(List columnNames, List rows)
Builds an HTML table from supplied arguments.
if (columnNames == null || rows == null) {
    return "";
StringBuilder sb = new StringBuilder();
sb.append("<table>");
sb.append("<tr>");
for (String column : columnNames) {
    sb.append("<th>" + column + "</th>");
...