Interface TableColumnModelExt
-
- All Superinterfaces:
TableColumnModel
- All Known Implementing Classes:
DefaultTableColumnModelExt
public interface TableColumnModelExt extends TableColumnModel
An extension ofTableColumnModel
suitable for use withJXTable
. It extends the notion of columns considered as part of the view realm to include invisible columns. Conceptually, there are several sets of "columns":- model columns: all columns of a
TableModel
. They are but a virtual concept, characterizable f.i. by (model) column index, (model) column name. - view columns: all
TableColumnExt
objects added to theTableColumnModelExt
, each typically created and configured in relation to a model column. These can be regarded as a kind of subset of the model columns (not literally, obviously). Each view column belongs to exactly one of the following (real) subsets:- visible columns: all view columns with the visibility property enabled
- hidden columns: all view columns with the visibility property disabled
ColumnControlButton
.An example to programmatically hide the first visible column in the column model:
Note that it is principally allowed to add standardTableColumnExt columnExt = columnModel.getColumnExt(0); if (columnExt != null) { columnExt.setVisible(false); }
TableColumn
s. Practically, it doesn't make much sense to do so - they will always be visible.While individual visible columns can be requested by both column identifier and column index, the latter is not available for hidden columns. An example to programmatically guarantee that the view column which corresponds to the first column in the associated
TableModel
.
Alternatively, the column could be requested directly by identifier. By default the column's headerValue is returned as identifier, if none is set.List<TableColumn> columns = colModel.getColumns(true); for (TableColumn column : columns) { if (column.getModelIndex() == 0) { if (column instanceof TableColumnExt) { ((TableColumnExt) column).setVisible(false); } return; } }
Relying on default identifiers is inherently brittle (Object identifier = tableModel.getColumnName(0); TableColumnExt columnExt = columnModel.getColumnExt(identifier); if (columnExt != null) { columnExt.setVisible(false); }
headerValue
s might change f.i. withLocale
s), so explicit configuration of columns with identifiers is strongly recommended. A customColumnFactory
helps to automate column configuration.This class guarantees to notify registered
TableColumnModelListener
s of typeTableColumnModelExtListener
about propertyChanges fired by containedTableColumn
s. An example of a client which adjusts itself based onheaderValue
property of visible columns:TableColumnModelExtListener l = new TableColumnModelExtListener() { public void columnPropertyChange(PropertyChangeEvent event) { if ("headerValue".equals(event.getPropertyName())) { TableColumn column = (TableColumn) event.getSource(); if ((column instanceof TableColumnExt) && !((TableColumnExt) column).isVisible()) { return; } resizeAndRepaint(); } } public void columnAdded(TableColumnModelEvent e) { } public void columnMarginChanged(ChangeEvent e) { } public void columnMoved(TableColumnModelEvent e) { } public void columnRemoved(TableColumnModelEvent e) { } public void columnSelectionChanged(ListSelectionEvent e) { } }; columnModel.addColumnModelListener(l);
- Author:
- Richard Bair, Jeanette Winzenburg
- See Also:
DefaultTableColumnModelExt
,TableColumnExt
,TableColumnModelExtListener
,ColumnControlButton
,JXTable.setColumnControlVisible(boolean)
,ColumnFactory
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
addColumnModelListener(TableColumnModelListener x)
Adds a listener for table column model events.int
getColumnCount(boolean includeHidden)
Returns the number of contained columns.TableColumnExt
getColumnExt(int columnIndex)
Returns theTableColumnExt
at view positioncolumnIndex
.TableColumnExt
getColumnExt(Object identifier)
Returns the firstTableColumnExt
with the givenidentifier
.List<TableColumn>
getColumns(boolean includeHidden)
Returns aList
of containedTableColumn
s.-
Methods inherited from interface javax.swing.table.TableColumnModel
addColumn, getColumn, getColumnCount, getColumnIndex, getColumnIndexAtX, getColumnMargin, getColumns, getColumnSelectionAllowed, getSelectedColumnCount, getSelectedColumns, getSelectionModel, getTotalColumnWidth, moveColumn, removeColumn, removeColumnModelListener, setColumnMargin, setColumnSelectionAllowed, setSelectionModel
-
-
-
-
Method Detail
-
getColumnCount
int getColumnCount(boolean includeHidden)
Returns the number of contained columns. The count includes or excludes invisible columns, depending on whether theincludeHidden
is true or false, respectively. If false, this method returns the same count asgetColumnCount()
.- Parameters:
includeHidden
- a boolean to indicate whether invisible columns should be included- Returns:
- the number of contained columns, including or excluding the invisible as specified.
-
getColumns
List<TableColumn> getColumns(boolean includeHidden)
Returns aList
of containedTableColumn
s. Includes or excludes invisible columns, depending on whether theincludeHidden
is true or false, respectively. If false, anIterator
over the List is equivalent to theEnumeration
returned bygetColumns()
.NOTE: the order of columns in the List depends on whether or not the invisible columns are included, in the former case it's the insertion order in the latter it's the current order of the visible columns.
- Parameters:
includeHidden
- a boolean to indicate whether invisible columns should be included- Returns:
- a
List
of contained columns.
-
getColumnExt
TableColumnExt getColumnExt(Object identifier)
Returns the firstTableColumnExt
with the givenidentifier
. The return value is null if there is no contained column with identifier or if the column withidentifier
is not of typeTableColumnExt
. The returned column may be visible or hidden.- Parameters:
identifier
- the object used as column identifier- Returns:
- first
TableColumnExt
with the given identifier or null if none is found
-
getColumnExt
TableColumnExt getColumnExt(int columnIndex)
Returns theTableColumnExt
at view positioncolumnIndex
. The return value is null, if the column at positioncolumnIndex
is not of typeTableColumnExt
. The returned column is visible.- Parameters:
columnIndex
- the index of the column desired- Returns:
- the
TableColumnExt
object that matches the column index - Throws:
ArrayIndexOutOfBoundsException
- if columnIndex out of allowed range, that is if(columnIndex < 0) || (columnIndex >= getColumnCount())
.
-
addColumnModelListener
void addColumnModelListener(TableColumnModelListener x)
Adds a listener for table column model events. This enhances super's behaviour in that it guarantees to notify listeners of type TableColumnModelListenerExt about property changes of contained columns.- Specified by:
addColumnModelListener
in interfaceTableColumnModel
- Parameters:
x
- aTableColumnModelListener
object
-
-