Class ColumnFactory
- java.lang.Object
-
- org.jdesktop.swingx.table.ColumnFactory
-
- Direct Known Subclasses:
- CustomColumnFactory
public class ColumnFactory extends java.lang.ObjectCreates and configuresTableColumnExts.TODO JW: explain types of configuration - initial from tableModel, initial from table context, user triggered at runtime.
JXTabledelegates allTableColumncreation and configuration to aColumnFactory. Enhanced column configuration should be implemented in a custom factory subclass. The example beautifies the column titles to always start with a capital letter:
By default a single instance is shared across all tables of an application. This instance can be replaced by a custom implementation, preferably "early" in the application's lifetime.MyColumnFactory extends ColumnFactory { //@Override public void configureTableColumn(TableModel model, TableColumnExt columnExt) { super.configureTableColumn(model, columnExt); String title = columnExt.getTitle(); title = title.substring(0,1).toUpperCase() + title.substring(1).toLowerCase(); columnExt.setTitle(title); } };
Alternatively, any instance ofColumnFactory.setInstance(new MyColumnFactory());JXTablecan be configured individually with its ownColumnFactory.JXTable table = new JXTable(); table.setColumnFactory(new MyColumnFactory()); table.setModel(myTableModel);- See Also:
JXTable.setColumnFactory(ColumnFactory)
-
-
Constructor Summary
Constructors Constructor and Description ColumnFactory()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidconfigureColumnWidths(JXTable table, TableColumnExt columnExt)Configures column initial widths properties fromJXTable.voidconfigureTableColumn(javax.swing.table.TableModel model, TableColumnExt columnExt)Configure column properties from TableModel.TableColumnExtcreateAndConfigureTableColumn(javax.swing.table.TableModel model, int modelIndex)Creates and configures a TableColumnExt.TableColumnExtcreateTableColumn(int modelIndex)Creates a table column with modelIndex.intgetDefaultPackMargin()Returns the default pack margin.static ColumnFactorygetInstance()Returns the shared default factory.intgetPreferredScrollableViewportWidth(JXTable table)Calculates and returns the preferred scrollable viewport width of the given table.voidpackColumn(JXTable table, TableColumnExt columnExt, int margin, int max)Configures the column'spreferredWidthto fit the content.voidsetDefaultPackMargin(int margin)Sets the default pack margin.static voidsetInstance(ColumnFactory factory)Sets the shared default factory.
-
-
-
Method Detail
-
getInstance
public static ColumnFactory getInstance()
Returns the shared default factory.- Returns:
- the shared instance of
ColumnFactory - See Also:
setInstance(ColumnFactory)
-
setInstance
public static void setInstance(ColumnFactory factory)
Sets the shared default factory. The shared instance is used byJXTableif none has been set individually.- Parameters:
factory- the default column factory.- See Also:
getInstance(),JXTable.getColumnFactory()
-
createAndConfigureTableColumn
public TableColumnExt createAndConfigureTableColumn(javax.swing.table.TableModel model, int modelIndex)
Creates and configures a TableColumnExt.JXTablecalls this method for each column in theTableModel.- Parameters:
model- the TableModel to read configuration properties frommodelIndex- column index in model coordinates- Returns:
- a TableColumnExt to use for the modelIndex
- Throws:
NPE- if model == nulljava.lang.IllegalStateException- if the modelIndex is invalid (in coordinate space of the tablemodel)- See Also:
createTableColumn(int),configureTableColumn(TableModel, TableColumnExt),JXTable.createDefaultColumnsFromModel()
-
createTableColumn
public TableColumnExt createTableColumn(int modelIndex)
Creates a table column with modelIndex.The factory's column creation is passed through this method, so subclasses can override to return custom column types.
- Parameters:
modelIndex- column index in model coordinates- Returns:
- a TableColumnExt with
modelIndex - See Also:
createAndConfigureTableColumn(TableModel, int)
-
configureTableColumn
public void configureTableColumn(javax.swing.table.TableModel model, TableColumnExt columnExt)Configure column properties from TableModel. This implementation sets the column'sheaderValueproperty from the model'scolumnName.The factory's initial column configuration is passed through this method, so subclasses can override to customize.
- Parameters:
model- the TableModel to read configuration properties fromcolumnExt- the TableColumnExt to configure.- Throws:
java.lang.NullPointerException- if model or column == nulljava.lang.IllegalStateException- if column does not have valid modelIndex (in coordinate space of the tablemodel)- See Also:
createAndConfigureTableColumn(TableModel, int)
-
configureColumnWidths
public void configureColumnWidths(JXTable table, TableColumnExt columnExt)
Configures column initial widths properties fromJXTable. This implementation sets the column'spreferredWidthwith the strategy:- if the column has a prototype, measure the rendering
component with the prototype as value and use that as
pref width
- if the column has no prototype, use the standard magic
pref width (= 75)
- try to measure the column's header and use it's preferred
width if it exceeds the former.
TODO JW - how to handle default settings which are different from standard 75?
- Parameters:
table- the context the column will live in.columnExt- the Tablecolumn to configure.- See Also:
JXTable.getPreferredScrollableViewportSize()
-
getPreferredScrollableViewportWidth
public int getPreferredScrollableViewportWidth(JXTable table)
Calculates and returns the preferred scrollable viewport width of the given table. Subclasses are free to override and implement a custom strategy.This implementation sums the pref widths of the first visibleColumnCount contained visible tableColumns. If the table contains less columns, the standard preferred width per column is added to the result.
- Parameters:
table- the table containing the columns
-
packColumn
public void packColumn(JXTable table, TableColumnExt columnExt, int margin, int max)
Configures the column'spreferredWidthto fit the content. It respects the table context, a margin to add and a maximum width. This is typically called in response to a user gesture to adjust the column's width to the "widest" cell content of a column.This implementation loops through all rows of the given column and measures the renderers pref width (it's a potential performance sink). Subclasses can override to implement a different strategy.
Note: though 2 * margin is added as spacing, this does not imply a left/right symmetry - it's up to the table to place the renderer and/or the renderer/highlighter to configure a border.
PENDING: support pack for hidden column? This implementation can't handle it! For now, added doc and fail-fast.
- Parameters:
table- the context the column will live in.columnExt- the column to configure.margin- the extra spacing to add twice, if -1 uses this factories defaultmax- an upper limit to preferredWidth, -1 is interpreted as no limit- Throws:
java.lang.IllegalStateException- if column is not visible- See Also:
setDefaultPackMargin(int),JXTable.packTable(int),JXTable.packColumn(int, int)
-
getDefaultPackMargin
public int getDefaultPackMargin()
Returns the default pack margin.- Returns:
- the default pack margin to use in packColumn.
- See Also:
setDefaultPackMargin(int)
-
setDefaultPackMargin
public void setDefaultPackMargin(int margin)
Sets the default pack margin.Note: this is not really a margin in the sense of symmetrically adding white space to the left/right of a cell's content. It's simply an amount of space which is added twice to the measured widths in packColumn.
- Parameters:
margin- the default marging to use in packColumn.- See Also:
getDefaultPackMargin(),packColumn(JXTable, TableColumnExt, int, int)
-
-
DataMelt 3.0 © DataMelt by jWork.ORG