Class ValueClassColumnWidthCalculator

  • All Implemented Interfaces:
    ColumnWidthCalculator

    public class ValueClassColumnWidthCalculator
    extends java.lang.Object
    implements ColumnWidthCalculator

    Calculates a column's width based on the value returned by JTable.getColumnClass(int). Intended for use with a table model that overrides AbstractTableModel.getColumnClass(int) because that method always returns Object.class. By default, the column cell renderer is used with the value specified below to determine the width.

    Column ClassRenderer ValueFixed Width
    BooleanColumn headertrue
    NumberConfiguration prototypetrue
    EnumLongest toString() valuetrue
    ColorSquaretrue
    otherColumn headerfalse
    See Also:
    BeanListTableModel
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<java.lang.Class<?>,​java.util.function.ToIntFunction<javax.swing.table.TableColumn>> calculatorMap  
      protected ColumnConfiguration configuration  
      protected io.github.jonestimd.swing.table.ValueClassColumnWidthCalculator.WidthCalculator headerWidthCalculator  
      protected javax.swing.JTable table  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isFixedWidth​(javax.swing.table.TableColumn column)
      The default implementation returns true if a width calculator has been set for the column.
      int preferredWidth​(javax.swing.table.TableColumn column)
      Calculate the preferred width of a column.
      protected void setDefaultCalculators()  
      void setWidthCalculator​(java.lang.Class<?> valueType, java.util.function.Function<javax.swing.table.TableColumn,​?> getPrototype)
      Use the header width for a prototype value as the column width.
      void setWidthCalculator​(java.lang.Class<?> valueType, java.util.function.Function<javax.swing.table.TableColumn,​?> getPrototype, java.util.function.ToIntFunction<java.awt.Component> getWidth)
      Calculate the column width using the header renderer for a prototype value.
      void setWidthCalculator​(java.lang.Class<?> valueType, java.util.function.ToIntFunction<javax.swing.table.TableColumn> calculator)
      Set the calculator for a column value type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • headerWidthCalculator

        protected final io.github.jonestimd.swing.table.ValueClassColumnWidthCalculator.WidthCalculator headerWidthCalculator
      • calculatorMap

        protected final java.util.Map<java.lang.Class<?>,​java.util.function.ToIntFunction<javax.swing.table.TableColumn>> calculatorMap
      • table

        protected final javax.swing.JTable table
    • Constructor Detail

      • ValueClassColumnWidthCalculator

        public ValueClassColumnWidthCalculator​(javax.swing.JTable table,
                                               ColumnConfiguration configuration)
    • Method Detail

      • setDefaultCalculators

        protected void setDefaultCalculators()
      • setWidthCalculator

        public void setWidthCalculator​(java.lang.Class<?> valueType,
                                       java.util.function.ToIntFunction<javax.swing.table.TableColumn> calculator)
        Set the calculator for a column value type.
      • setWidthCalculator

        public void setWidthCalculator​(java.lang.Class<?> valueType,
                                       java.util.function.Function<javax.swing.table.TableColumn,​?> getPrototype)
        Use the header width for a prototype value as the column width.
        Parameters:
        getPrototype - a function that returns the prototype value for a column
      • setWidthCalculator

        public void setWidthCalculator​(java.lang.Class<?> valueType,
                                       java.util.function.Function<javax.swing.table.TableColumn,​?> getPrototype,
                                       java.util.function.ToIntFunction<java.awt.Component> getWidth)
        Calculate the column width using the header renderer for a prototype value.
        Parameters:
        getPrototype - a function that returns the prototype value for a column
        getWidth - a function that returns the width for the column given the header renderer
      • preferredWidth

        public int preferredWidth​(javax.swing.table.TableColumn column)
        Description copied from interface: ColumnWidthCalculator
        Calculate the preferred width of a column.
        Specified by:
        preferredWidth in interface ColumnWidthCalculator
        Parameters:
        column - the table column
        Returns:
        the preferred width of the column