Class JXTableHeader

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, TableColumnModelListener, TableColumnModelExtListener

public class JXTableHeader extends JTableHeader implements TableColumnModelExtListener
TableHeader with extended functionality if associated Table is of type JXTable.

Extended user interaction

  • Supports column pack (== auto-resize to exactly fit the contents) on double-click in resize region.
  • Configurable to resort a column on the second click of a mouseClicked event (feature request #271-swingx)
  • Does its best to not sort if the mouse click happens in the resize region.
  • Supports horizontal auto-scroll if a column is dragged outside visible rectangle. This feature is enabled if the autoscrolls property is true. The default is false (because of Issue #788-swingx which still isn't fixed for jdk1.6).
Note: extended sort and resize related functionality is fully effective only if the header's table is of type JXTable and has control over the row sorter, that is the row sorter is of type SortController.

Extended functionality

  • Listens to TableColumn propertyChanges to update itself accordingly.
  • Supports per-column header ToolTips.
  • Guarantees reasonable minimal height > 0 for header preferred height.
Author:
Jeanette Winzenburg
See Also:
  • Constructor Details

    • JXTableHeader

      public JXTableHeader()
      Constructs a JTableHeader with a default TableColumnModel.
      See Also:
    • JXTableHeader

      public JXTableHeader(TableColumnModel columnModel)
      Constructs a JTableHeader which is initialized with cm as the column model. If cm is null this method will initialize the table header with a default TableColumnModel.
      Parameters:
      columnModel - the column model for the table
      See Also:
  • Method Details

    • setTable

      public void setTable(JTable table)

      Sets the associated JTable. Enables enhanced header features if table is of type JXTable.

      PENDING: who is responsible for synching the columnModel?

      Overrides:
      setTable in class JTableHeader
    • installTable

      protected void installTable()
      Installs the table.

      This implemenation synchs enabled state and installs the PropertyChangeListener.

    • updateEnabledFromTable

      protected void updateEnabledFromTable()
      Synchs the header's enabled with the table's enabled property.
    • uninstallTable

      protected void uninstallTable()
      Uninstalls the table.

      This implementation uninstalls the PropertyChangeListener.

    • columnPropertyChange

      public void columnPropertyChange(PropertyChangeEvent event)
      Implements TableColumnModelExt to allow internal update after column property changes.

      This implementation triggers a resizeAndRepaint on every propertyChange which doesn't already fire a "normal" columnModelEvent.

      Specified by:
      columnPropertyChange in interface TableColumnModelExtListener
      Parameters:
      event - change notification from a contained TableColumn.
      See Also:
    • isColumnEvent

      protected boolean isColumnEvent(PropertyChangeEvent event)
      Returns a boolean indicating if a property change event received from column changes is expected to be already broadcasted by the core TableColumnModel.

      This implementation returns true for notification of width, preferredWidth and visible properties, false otherwise.

      Parameters:
      event - the PropertyChangeEvent received as TableColumnModelExtListener.
      Returns:
      a boolean to decide whether the same event triggers a base columnModelEvent.
    • getPopupLocation

      public Point getPopupLocation(MouseEvent event)

      Overridden for bookkeeping: the given event location is stored for later access.

      Overrides:
      getPopupLocation in class JComponent
      See Also:
    • updatePopupTrigger

      protected void updatePopupTrigger(MouseEvent event)
      Handles internal bookkeeping related to popupLocation, called from getPopupLocation.

      This implementation stores the mouse location as popupTriggerLocation.

      Parameters:
      event - the event that triggered the showing of the componentPopup, might be null if triggered by keyboard
    • getPopupTriggerLocation

      public Point getPopupTriggerLocation()
      Returns the location of the mouseEvent that triggered the showing of the ComponentPopupMenu.
      Returns:
      the location of the mouseEvent that triggered the last showing of the ComponentPopup, or null if it was triggered by keyboard.
    • getToolTipText

      public String getToolTipText(MouseEvent event)

      Overridden to respect the column tooltip, if available.

      Overrides:
      getToolTipText in class JTableHeader
      Returns:
      the column tooltip of the column at the mouse position if not null or super if not available.
    • getColumnToolTipText

      protected String getColumnToolTipText(MouseEvent event)
      Returns the column tooltip of the column at the position of the MouseEvent, if a tooltip is available.
      Parameters:
      event - the mouseEvent representing the mouse location.
      Returns:
      the column tooltip of the column below the mouse location, or null if not available.
    • getColumnToolTipText

      protected String getColumnToolTipText(TableColumnExt columnExt)
      Returns the column tooltip for the given column or null if none available.

      This implementation returns the toolTipText property of the given column.

      Parameters:
      columnExt - the column to return the tooltip for, must not be null
      Returns:
      the tooltip for the column or null if none available.
      See Also:
    • getColumnExt

      public TableColumnExt getColumnExt(Point point)
      Returns the tableColumn at the given location or null if the location is outside or the column is not of type TableColumnExt.
      Parameters:
      point - the location to return the column for
      Returns:
      the tableColumn at the location or null
    • getXTable

      public JXTable getXTable()
      Returns the associated table if it is of type JXTable, or null if not.
      Returns:
      the associated table if of type JXTable or null if not.
    • getResortsOnDoubleClick

      public boolean getResortsOnDoubleClick()
      Returns the resortsOnDoubleClick property.
      Returns:
      a flag indicating whether or not the second click in a mouseClicked event should toggle the sort order again.
      See Also:
    • setResortsOnDoubleClick

      public void setResortsOnDoubleClick(boolean resortsOnDoubleClick)
      Sets the resortsOnDoubleClick property. If enabled, the second click of a mouseClicked event will toggle the sort order again if the column has been unsorted before. This is introduced to support feature request #271-swingx. It is effective only if the coupled table is of type JXTable and has full control about its RowSorter's properties. The default value is false.
      Parameters:
      resortsOnDoubleClick - a boolean indicating whether or not the second click in a mouseClicked event should resort the column.
      See Also:
    • getCellRenderer

      public TableCellRenderer getCellRenderer(int columnIndex)
      Returns the TableCellRenderer to use for the column with the given index. This implementation returns the column's header renderer if available or this header's default renderer if not.
      Parameters:
      columnIndex - the index in view coordinates of the column
      Returns:
      the renderer to use for the column, guaranteed to be not null.
    • getPreferredSize

      public Dimension getPreferredSize()

      Overridden to adjust for a reasonable minimum height. Done to fix Issue 334-swingx, which actually is a core issue misbehaving in returning a zero height if the first column has no text.

      Overrides:
      getPreferredSize in class JTableHeader
      See Also:
    • getPreferredSize

      protected Dimension getPreferredSize(Dimension pref)
      Returns a preferred size which is adjusted to the maximum of all header renderers' height requirement.
      Parameters:
      pref - an initial preferred size
      Returns:
      the initial preferred size with its height property adjusted to the maximum of all renderers preferred height requirement.
      See Also:
    • getMinimumHeight

      protected int getMinimumHeight(int height)
      Returns a reasonable minimal preferred height for the header. This is meant as a last straw if all header values are null, renderers report 0 as their preferred height.

      This implementation returns the default header renderer's preferred height as measured with a dummy value if the input height is 0, otherwise returns the height unchanged.

      Parameters:
      height - the initial height.
      Returns:
      a reasonable minimal preferred height.
      See Also:
    • setDraggedColumn

      public void setDraggedColumn(TableColumn column)

      Overridden to fire a propertyChange for draggedColumn.

      Overrides:
      setDraggedColumn in class JTableHeader
    • setResizingColumn

      public void setResizingColumn(TableColumn aColumn)

      Overridden to fire a propertyChange for resizingColumn.

      Overrides:
      setResizingColumn in class JTableHeader
    • setDraggedDistance

      public void setDraggedDistance(int distance)

      Overridden to scroll the table to keep the dragged column visible. This side-effect is enabled only if the header's autoscroll property is true and the associated table is of type JXTable.

      The autoscrolls is disabled by default. With or without - core issue #6503981 has weird effects (for jdk 1.6 - 1.6u3) on a plain JTable as well as a JXTable, fixed in 1.6u4.

      Overrides:
      setDraggedDistance in class JTableHeader
    • getDraggedColumn

      public TableColumn getDraggedColumn()
      Returns the the dragged column if and only if, a drag is in process and the column is visible, otherwise returns null.
      Overrides:
      getDraggedColumn in class JTableHeader
      Returns:
      the dragged column, if a drag is in process and the column is visible, otherwise returns null
      See Also:
    • getTablePropertyChangeListener

      protected PropertyChangeListener getTablePropertyChangeListener()
      Returns the PropertyChangeListener to register on the owning table, lazily created.
      Returns:
      the PropertyChangeListener to use on the owning table.
    • createTablePropertyChangeListener

      protected PropertyChangeListener createTablePropertyChangeListener()
      Creates and returns the PropertyChangeListener to register on the owning table.

      This implementation synchs the header's enabled properties with the table's enabled.

      Returns:
      the PropertyChangeListener to register on the owning table.
    • installHeaderListener

      protected void installHeaderListener()
      Creates and installs header listeners to service the extended functionality. This implementation creates and installs a custom mouse input listener.
    • uninstallHeaderListener

      protected void uninstallHeaderListener()
      Uninstalls header listeners to service the extended functionality. This implementation uninstalls a custom mouse input listener.