Holger's Java API

## com.antelmann.util.table
Class TableUtils

    java.lang.Object
      com.antelmann.util.table.TableUtils

    public class TableUtils

extends Object

some utilities for tables

Author:
  Holger Antelmann

Nested Class Summary
`static class` `TableUtils.BooleanRenderer` thanks for `JTable` for not making this available externally :(

Method Summary
`static TableModelListener` `addRowHeightAdjuster(JTable table)` adds a TableModelListener to the table's model, which adjusts row heights when data changes
`static void` `adjustColumnWidths(int maxRowsToConsider, JTable table, int... columnToAdjust)` adjust the widths of the columns so that the header and the given number of rows fit into the cell.
`static void` `adjustColumnWidths(JTable table)` adjust the widths of the columns so that all content fits into the cell properly
`static void` `adjustRowHeights(JTable table)` adjusts the height of every row for the entire table.
`static void` `adjustRowHeights(JTable table, int beginRow, int endRow)` adjusts the height of the given rows based on the maximum preferred height of each renderer for any given row.
`static void` `copy(TableModel source, Rectangle sourceRegion, TableModel target, Point targetBegin)` copies the sourceRegion from the source into the target beginning at the given point
`static void` `copy(TableModel source, TableModel target)` copies the entire source table into the target table
`static CounterMap<Object>` `count(TableModel model, int... columns)`
`static CounterMap<Object>` `count(TableModel model, String... columns)`
`static DefaultTableModel` `createSampleModel(int rowCount)` provides a quick sample model for testing purposes
`static JTable` `createStretchedTable()` creates a table that 'stretches' over the viewport of a JScrollPane, even if the table itself is smaller than that.
`static JTable` `createStretchedTable(TableModel model)` creates a table that 'stretches' over the viewport of a JScrollPane, even if the table itself is smaller than that.
`static void` `delegateRenderingToModel(JTable table)`
`static void` `delegateRenderingToModel(JTable table, TableCellRenderer defaultRenderer)` sets all columns of the given table to be rendered by a `TableModelRenderer`
`static void` `editColumnAsPercent(JTable table, int column)` sets appropriate editor and renderer for the given column, so that it can be displayed/edited as a percent value in a consistent way
`static void` `editColumnAsPercent(JTable table, int column, Locale l)` sets appropriate editor and renderer for the given column, so that it can be displayed/edited as a percent value in a consistent way
`static void` `enableEditAction(JTable table, ActionListener al, String actionName)` sets the given ActionListener to be activated upon either double-mouse-click or pressing enter when a table row is selected
`static void` `enableToolTips(JTable table)` wraps any given TableCellRenderer with a ToolTipRenderer in all columns
`static void` `enableToolTipsForHeader(JTable table)` sets the column name as the tool tip for all column headers
`static JPanel` `fitToPanel(JTable table, boolean showHeader)` creates a special JPanel that is filled with the given table while the row height adjusts automatically to the size of the JPanel, so that all rows have equal space in the component.
`static JTable` `fixColumns(JScrollPane scrollPane, int fixedColumns)` fixes the first given number of columns in the given JScrollPane so that these first columns won't scroll horizontally anymore.
`static TableCellEditor` `getColorEditor()` edits Color objects
`static TableCellRenderer` `getColorRenderer()` renders Color objects
`static int` `getColumn(TableModel model, String columnName)` if the column name is not found, -1 is returned
`static String[]` `getColumnNames(TableModel model)`
`static HashSet<Object>` `getColumnValues(TableModel model, int column)`
`static Component` `getComponentFor(AbstractTableModel model)` useful to show a dialog from within a TableModel (while setting a value, for instance), where no Component is readily available.
`static CurrencyCellRenderer` `getCurrencyRenderer(boolean useToolTipText)`
`static FormattedCellEditor` `getDateEditor()` edits Date objects
`static TableCellEditor` `getFontEditor()` edits Font objects
`static TableCellRenderer` `getFontRenderer()` renders Font objects
`static int` `getMaxPreferredRowHeight(JTable table)`
`static int` `getMaxPreferredRowHeight(JTable table, int beginRow, int endRow)`
`static TableCellRenderer` `getMultiRenderer()`
`static FilteredTableModel` `getSelectedRowsAsModel(JTable table)`
`static TimeCellRenderer` `getTimeRenderer(boolean useToolTipText)` renders Date and Long objects
`static DefaultTableCellRenderer` `getToolTipRenderer()` provides a tool tip based on the toString() method of the cell object.
`static DefaultTableCellRenderer` `getToolTipRenderer(TableCellRenderer renderer)` provides a tool tip based on the toString() method of the cell object; otherwise, the given formatter is used
`static void` `makeColumnWithAdjustable(JTable table)` adds a mouse listener to the header that will resize the column width on double-click on the header
`static JTable` `makePropertiesTable(Map<?,?> properties)` calls `makePropertiesTable(properties, "key", "value")`
`static JTable` `makePropertiesTable(Map<?,?> properties, String keyHeader, String valueHeader)`
`static SortableTableModel` `makeSortable(JTable table, Comparator<?>... comp)`
`static SortableTableModel` `makeSortable(JTable table, List<Comparator<?>> comp)` wraps the TableModel with a SortableTableModel, replaces the model of the table with the same and registers a MouseListener on the table header to support sorting.
`static DefaultTableModel` `makeTableModel(Map<?,?> map)`
`static DefaultTableModel` `makeTableModel(Map<?,?> map, String keyHeader, String valueHeader)` creates a read-only model
`static TableModelWrapper` `readOnly(TableModel model)`
`static boolean` `removeSortableListener(JTable table)`
`static HashMap<String,Object>` `rowToMap(TableModel model, int row)`
`static void` `scrollToRow(JTable table, int row)` convenience method
`static TableCellEditor` `selectAllTableCellEditor(TableCellEditor editor)` wraps the given editor into one that will select all text upon editing, provided that the given editor will use a JTextComponent as a Component.
`static void` `setTerminateEditOnFocusLost(JTable table, boolean flag)` setting this to true fixes an annoying problem of editor values not being committed when the table looses focus.
`static void` `setToolTipForHeader(JTable table, int columnIndex, String tooltip)` for more specific header rendering options, see `LabelRenderer`
`static void` `setToolTipForHeader(JTable table, String columnName, String tooltip)` for more specific header rendering options, see `LabelRenderer`
`static boolean` `stopCellEditing(JTable table)` useful to ensure that all cells stop editing before another action relies on the entered values
`static String` `toString(TableModel model, boolean includeHeader)`
`static String` `toString(TableModel model, boolean includeHeader, String columnDelimiter, String rowDelimiter)`
`static void` `wrapEditorToSelectAll(JTable table, int column)` wraps the TableCellEditor of the given column into one that will select all on editing

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### getMultiRenderer

    public static TableCellRenderer getMultiRenderer()

### getColorRenderer

    public static TableCellRenderer getColorRenderer()

  renders Color objects

### getColorEditor

    public static TableCellEditor getColorEditor()

  edits Color objects

### getDateEditor

    public static FormattedCellEditor getDateEditor()

  edits Date objects

### getFontRenderer

    public static TableCellRenderer getFontRenderer()

  renders Font objects

### getFontEditor

    public static TableCellEditor getFontEditor()

  edits Font objects

### getTimeRenderer

    public static TimeCellRenderer getTimeRenderer(boolean useToolTipText)

  renders Date and Long objects

### getCurrencyRenderer

    public static CurrencyCellRenderer getCurrencyRenderer(boolean useToolTipText)

### getToolTipRenderer

    public static DefaultTableCellRenderer getToolTipRenderer()

  provides a tool tip based on the toString() method of the cell object. To support multiple lines, the String is encoded as HTML.

  See Also:
    `Strings.encodeXML(String)`

### getToolTipRenderer

    public static DefaultTableCellRenderer getToolTipRenderer(TableCellRenderer renderer)

  provides a tool tip based on the toString() method of the cell object; otherwise, the given formatter is used

### delegateRenderingToModel

    public static void delegateRenderingToModel(JTable table)

  See Also:
    `delegateRenderingToModel(JTable, TableCellRenderer)`

### delegateRenderingToModel

    public static void delegateRenderingToModel(JTable table,
                                                TableCellRenderer defaultRenderer)

  sets all columns of the given table to be rendered by a `TableModelRenderer`

  See Also:
    `TableModelRenderer`

### setTerminateEditOnFocusLost

    public static void setTerminateEditOnFocusLost(JTable table,
                                                   boolean flag)

  setting this to true fixes an annoying problem of editor values not being committed when the table looses focus. For very odd reasons, the default is false, though it is practically always a good idea to set this to true. Note, however, that this does not solve all problems related to cell values not being committed to the model (even as of JDK 1.6); see Bug 4709394 and Bug 4724980 and Bug 4330950 at Sun's website for reference. Also, this and this page contain useful additional information regarding the subject.

  See Also:
    `stopCellEditing(JTable)`

### stopCellEditing

    public static boolean stopCellEditing(JTable table)

  useful to ensure that all cells stop editing before another action relies on the entered values

  Returns:
    true if editing was/is stopped
  See Also:
    `setTerminateEditOnFocusLost(JTable, boolean)`

### enableToolTips

    public static void enableToolTips(JTable table)

  wraps any given TableCellRenderer with a ToolTipRenderer in all columns

### enableToolTipsForHeader

    public static void enableToolTipsForHeader(JTable table)

  sets the column name as the tool tip for all column headers

  See Also:
    `setToolTipForHeader(JTable, int, String)`

### setToolTipForHeader

    public static void setToolTipForHeader(JTable table,
                                           String columnName,
                                           String tooltip)

  for more specific header rendering options, see `LabelRenderer`

### setToolTipForHeader

    public static void setToolTipForHeader(JTable table,
                                           int columnIndex,
                                           String tooltip)

  for more specific header rendering options, see `LabelRenderer`

### editColumnAsPercent

    public static void editColumnAsPercent(JTable table,
                                           int column)

  sets appropriate editor and renderer for the given column, so that it can be displayed/edited as a percent value in a consistent way

### editColumnAsPercent

    public static void editColumnAsPercent(JTable table,
                                           int column,
                                           Locale l)

  sets appropriate editor and renderer for the given column, so that it can be displayed/edited as a percent value in a consistent way

### wrapEditorToSelectAll

    public static void wrapEditorToSelectAll(JTable table,
                                             int column)

  wraps the TableCellEditor of the given column into one that will select all on editing

  See Also:
    `selectAllTableCellEditor(TableCellEditor)`

### selectAllTableCellEditor

    public static TableCellEditor selectAllTableCellEditor(TableCellEditor editor)

  wraps the given editor into one that will select all text upon editing, provided that the given editor will use a JTextComponent as a Component.

### adjustColumnWidths

    public static void adjustColumnWidths(JTable table)

  adjust the widths of the columns so that all content fits into the cell properly

### adjustColumnWidths

    public static void adjustColumnWidths(int maxRowsToConsider,
                                          JTable table,
                                          int... columnToAdjust)

  adjust the widths of the columns so that the header and the given number of rows fit into the cell. If maxRowsToConsider is 0, only the header is considered; if negative, all rows are considered. If maxRowsToConsider is greater than the number of rows, simply all rows will be considered. If no column is specified to be adjusted, all columns are adjusted.

### getComponentFor

    public static Component getComponentFor(AbstractTableModel model)

  useful to show a dialog from within a TableModel (while setting a value, for instance), where no Component is readily available.

### readOnly

    public static TableModelWrapper readOnly(TableModel model)

### rowToMap

    public static HashMap<String,Object> rowToMap(TableModel model,
                                                  int row)

### makePropertiesTable

    public static JTable makePropertiesTable(Map<?,?> properties)

  calls `makePropertiesTable(properties, "key", "value")`

### makePropertiesTable

    public static JTable makePropertiesTable(Map<?,?> properties,
                                             String keyHeader,
                                             String valueHeader)

### makeTableModel

    public static DefaultTableModel makeTableModel(Map<?,?> map)

### makeTableModel

    public static DefaultTableModel makeTableModel(Map<?,?> map,
                                                   String keyHeader,
                                                   String valueHeader)

  creates a read-only model

### removeSortableListener

    public static boolean removeSortableListener(JTable table)

  See Also:
    `makeSortable(JTable, List)`

### makeSortable

    public static SortableTableModel makeSortable(JTable table,
                                                  Comparator<?>... comp)

### makeSortable

    public static SortableTableModel makeSortable(JTable table,
                                                  List<Comparator<?>> comp)

  wraps the TableModel with a SortableTableModel, replaces the model of the table with the same and registers a MouseListener on the table header to support sorting.

  Returns:
    the SortableTableModel that is now used by the table, which embeds the model previously used by the given table
  See Also:
    `SortableTableModel`, `removeSortableListener(JTable)`

### makeColumnWithAdjustable

    public static void makeColumnWithAdjustable(JTable table)

  adds a mouse listener to the header that will resize the column width on double-click on the header

### addRowHeightAdjuster

    public static TableModelListener addRowHeightAdjuster(JTable table)

  adds a TableModelListener to the table's model, which adjusts row heights when data changes

  Returns:
    the listener that has been added to the table's model
  See Also:
    `adjustRowHeights(javax.swing.JTable, int, int)`

### fitToPanel

    public static JPanel fitToPanel(JTable table,
                                    boolean showHeader)

  creates a special JPanel that is filled with the given table while the row height adjusts automatically to the size of the JPanel, so that all rows have equal space in the component.

### getMaxPreferredRowHeight

    public static int getMaxPreferredRowHeight(JTable table)

### getMaxPreferredRowHeight

    public static int getMaxPreferredRowHeight(JTable table,
                                               int beginRow,
                                               int endRow)

### adjustRowHeights

    public static void adjustRowHeights(JTable table)

  adjusts the height of every row for the entire table.

### adjustRowHeights

    public static void adjustRowHeights(JTable table,
                                        int beginRow,
                                        int endRow)

  adjusts the height of the given rows based on the maximum preferred height of each renderer for any given row.

### scrollToRow

    public static void scrollToRow(JTable table,
                                   int row)

  convenience method

### enableEditAction

    public static void enableEditAction(JTable table,
                                        ActionListener al,
                                        String actionName)

  sets the given ActionListener to be activated upon either double-mouse-click or pressing enter when a table row is selected

### createStretchedTable

    public static JTable createStretchedTable()

  creates a table that 'stretches' over the viewport of a JScrollPane, even if the table itself is smaller than that. This way, a JTable behaves just as a JTree or JList in that respect. Since JDK1.6, this behavior can alternatively be achieved by using JTable.setFillsViewportHeightsetFillsViewportHeight(boolean).

  Returns:
    a JTable that stretches over the size of a containing viewport

### createStretchedTable

    public static JTable createStretchedTable(TableModel model)

  creates a table that 'stretches' over the viewport of a JScrollPane, even if the table itself is smaller than that. This way, a JTable behaves just as a JTree or JList in that respect. Since JDK1.6, this behavior can alternatively be achieved by using JTable.setFillsViewportHeightsetFillsViewportHeight(boolean).

  Returns:
    a JTable that stretches over the size of a containing viewport

### getColumn

    public static int getColumn(TableModel model,
                                String columnName)

  if the column name is not found, -1 is returned

### copy

    public static void copy(TableModel source,
                            TableModel target)

  copies the entire source table into the target table

### copy

    public static void copy(TableModel source,
                            Rectangle sourceRegion,
                            TableModel target,
                            Point targetBegin)

  copies the sourceRegion from the source into the target beginning at the given point

### count

    public static CounterMap<Object> count(TableModel model,
                                           String... columns)

### count

    public static CounterMap<Object> count(TableModel model,
                                           int... columns)

### getColumnNames

    public static String[] getColumnNames(TableModel model)

### fixColumns

    public static JTable fixColumns(JScrollPane scrollPane,
                                    int fixedColumns)
                             throws IllegalArgumentException

  fixes the first given number of columns in the given JScrollPane so that these first columns won't scroll horizontally anymore. It creates a new table (that is returned) that contains the fixed columns The base for this code is from Rob Camick and was found on the Internet at http://tips4java.wordpress.com/2008/11/05/fixed-column-table/ - thank you.

  Parameters:
    `scrollPane` - the scroll pane that must have JTable as its viewport
    `fixedColumns` - the number of columns that are to be fixed on the left side
  Throws:
    `IllegalArgumentException`

### createSampleModel

    public static DefaultTableModel createSampleModel(int rowCount)

  provides a quick sample model for testing purposes

### toString

    public static String toString(TableModel model,
                                  boolean includeHeader)

### toString

    public static String toString(TableModel model,
                                  boolean includeHeader,
                                  String columnDelimiter,
                                  String rowDelimiter)

### getSelectedRowsAsModel

    public static FilteredTableModel getSelectedRowsAsModel(JTable table)

### getColumnValues

    public static HashSet<Object> getColumnValues(TableModel model,
                                                  int column)

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads