Java JTable Header isColumnSelected(JTableHeader tableHeader, int columnModelIndex)

Here you can find the source of isColumnSelected(JTableHeader tableHeader, int columnModelIndex)

Description

true if the given column model index is selected in the given JTableHeader .

License

Open Source License

Parameter

Parameter Description
tableHeader the JTableHeader to use when determining if the given column is selected.
columnModelIndex the column model index to determine the selected state of.

Return

true if the given column is selected.

Declaration

public static boolean isColumnSelected(JTableHeader tableHeader, int columnModelIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.table.JTableHeader;

public class Main {
    public static final int NO_COLUMN = -1;
    private static final String SELECTED_COLUMN_KEY = "EPJTableHeader.selectedColumn";

    /**//  w  w w . ja  v  a 2s . c o  m
     * {@code true} if the given column model index is selected in the given {@link JTableHeader}.
     * <p/>
     * Note that there is no direct way to set wheter a column is selected or not. Selection is a
     * by-product of set the sort direction on a column.
     *
     * @param tableHeader      the {@code JTableHeader} to use when determining if the given column is
     *                         selected.
     * @param columnModelIndex the column model index to determine the selected state of.
     * @return {@code true} if the given column is selected.
     * @see #getSelectedColumn(javax.swing.table.JTableHeader)
     * @see #setSortDirection(javax.swing.table.JTableHeader, int, com.jtechdev.widgets.TableUtils.SortDirection)
     */
    public static boolean isColumnSelected(JTableHeader tableHeader, int columnModelIndex) {
        int selectedColumn = getSelectedColumn(tableHeader);
        return selectedColumn >= 0 && selectedColumn == columnModelIndex;
    }

    /**
     * Get's the selected column header for the given {@link JTableHeader}.
     *
     * @param tableHeader the {@code JTableHeader} to determine the selected column header for.
     * @return the column model index of the selected column header, or {@link #NO_COLUMN} if no
     *         column's header has been pressed.
     * @see #isColumnSelected(javax.swing.table.JTableHeader, int)
     */
    public static int getSelectedColumn(JTableHeader tableHeader) {
        Object selectedColumnValue = tableHeader.getClientProperty(SELECTED_COLUMN_KEY);
        return selectedColumnValue != null && selectedColumnValue instanceof Integer
                ? ((Integer) selectedColumnValue)
                : NO_COLUMN;
    }
}

Related

  1. getMaxColumnWidth(JTable aTable, int columnNo, boolean includeColumnHeaderWidth, int columnPadding)
  2. getSelectedColumn(JTableHeader tableHeader)
  3. getTableHeaderBackground()
  4. getTableHeaderColor()
  5. headerString(JTable t)
  6. isRowHeaderVisible(JTable table)
  7. layoutHeaders(JTable p_Table)
  8. makeHeaderFillEmptySpace(JTable table)
  9. makeTableHeadersCentered(final JTable table, final boolean dataColsAlso)