get JTable Column Index From Name - Java Swing

Java examples for Swing:JTable Column

Description

get JTable Column Index From Name

Demo Code


//package com.java2s;
import javax.swing.table.TableModel;

public class Main {
    /**//from   ww  w.j a  va  2s.c o  m
     * @param tableModel
     * @param columnToRemove
     * @return the index of the column, where the column name is the string
     */
    public static int getColumnIndexFromName(TableModel tableModel,
            String columnToRemove) {
        for (int columnIndex = 0; columnIndex < tableModel.getColumnCount(); columnIndex++) {
            if (tableModel.getColumnName(columnIndex)
                    .equals(columnToRemove)) {
                return columnIndex;
            }

        }
        return -1;

    }
}

Related Tutorials