Returns true if column with columnName is displayed within the JTable. - Java Swing

Java examples for Swing:JTable Column

Description

Returns true if column with columnName is displayed within the JTable.

Demo Code


//package com.java2s;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.JTable;

import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Main {
    /**//  w  ww .j  a  va 2s.  c  o m
     * Returns true if column with columnName is displayed within the table.
     *
     * @param table The table.
     * @param columnName The columnName.
     * @return true if column with columnName is displayed within the table.
     */
    public static boolean isColumnDisplayed(JTable table, String columnName) {
        ArrayList<TableColumn> cols = getTableColumns(table);
        for (TableColumn tc : cols) {
            if (((String) tc.getHeaderValue()).equals(columnName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns The array list of table columns.
     *
     * @param table The table.
     * @return The array list of table columns.
     */
    public static ArrayList<TableColumn> getTableColumns(JTable table) {
        ArrayList<TableColumn> list = new ArrayList<TableColumn>();
        TableColumnModel model = table.getColumnModel();
        Enumeration<TableColumn> enumeration = model.getColumns();
        while (enumeration.hasMoreElements()) {
            TableColumn column = enumeration.nextElement();
            list.add(column);

        }
        return list;
    }
}

Related Tutorials