Class ResizeableTableHeader

  • All Implemented Interfaces:
    java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.TableColumnModelListener, javax.swing.event.TableModelListener

    public class ResizeableTableHeader
    extends javax.swing.table.JTableHeader
    implements javax.swing.event.TableModelListener
    This subclass of JTableHeader extends a JTable with the ability to resize a column to fit all its content on double click on the right end of the column's header cell.
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.table.JTableHeader

        javax.swing.table.JTableHeader.AccessibleJTableHeader
      • Nested classes/interfaces inherited from class javax.swing.JComponent

        javax.swing.JComponent.AccessibleJComponent
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean autoResizingEnabled
      If true, auto resizing of columns when the TableModel invokes a tableChanged event is enabled.
      protected boolean includeHeaderWidth
      If true, resizing of columns will also take the width of header cells into account.
      • Fields inherited from class javax.swing.table.JTableHeader

        columnModel, draggedColumn, draggedDistance, reorderingAllowed, resizingAllowed, resizingColumn, table, updateTableInRealTime
      • Fields inherited from class javax.swing.JComponent

        listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
      • Fields inherited from class java.awt.Component

        accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
      • Fields inherited from interface java.awt.image.ImageObserver

        ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
    • Constructor Summary

      Constructors 
      Constructor Description
      ResizeableTableHeader()
      Constructs a ResizeableTableHeader with a default TableColumnModel.
      ResizeableTableHeader​(javax.swing.table.TableColumnModel cm)
      Constructs a ResizeableTableHeader which is initialized with cm as the column model.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean getAutoResizingEnabled()
      Returns true if auto resizing is enabled.
      boolean getIncludeHeaderWidth()
      Returns true, if the header's width are
      void resizeAllColumns()
      Resizes all columns to fit all their content.
      void resizeColumn​(javax.swing.table.TableColumn column)
      Resizes the given column to fit all its content.
      void setAllColumnWidths​(int preferredWidth, int minWidth, int maxWidth)
      Sets preferred width, the minimum and maximum width for all columns.
      void setAutoResizingEnabled​(boolean autoResizingEnabled)
      Sets whether columns are resized on table model events.
      void setColumnWidths​(javax.swing.table.TableColumn column, int preferredWidth, int minWidth, int maxWidth)
      Sets preferred width, the minimum and maximum width for a given column.
      void setIncludeHeaderWidth​(boolean includeHeaderWidth)
      Sets whether the header's width are included on calculation
      void setTable​(javax.swing.JTable table)
      Sets the table associated with this header.
      void tableChanged​(javax.swing.event.TableModelEvent e)
      Listen for table model events from tablemodel.
      • Methods inherited from class javax.swing.table.JTableHeader

        columnAdded, columnAtPoint, columnMarginChanged, columnMoved, columnRemoved, columnSelectionChanged, createDefaultColumnModel, createDefaultRenderer, getAccessibleContext, getColumnModel, getDefaultRenderer, getDraggedColumn, getDraggedDistance, getHeaderRect, getPreferredSize, getReorderingAllowed, getResizingAllowed, getResizingColumn, getTable, getToolTipText, getUI, getUIClassID, getUpdateTableInRealTime, initializeLocalVars, paramString, resizeAndRepaint, setColumnModel, setDefaultRenderer, setDraggedColumn, setDraggedDistance, setReorderingAllowed, setResizingAllowed, setResizingColumn, setUI, setUpdateTableInRealTime, updateUI
      • Methods inherited from class javax.swing.JComponent

        addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

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

      • autoResizingEnabled

        protected boolean autoResizingEnabled
        If true, auto resizing of columns when the TableModel invokes a tableChanged event is enabled. The default is false.
      • includeHeaderWidth

        protected boolean includeHeaderWidth
        If true, resizing of columns will also take the width of header cells into account. The default is false.
    • Constructor Detail

      • ResizeableTableHeader

        public ResizeableTableHeader()
        Constructs a ResizeableTableHeader with a default TableColumnModel.
      • ResizeableTableHeader

        public ResizeableTableHeader​(javax.swing.table.TableColumnModel cm)
        Constructs a ResizeableTableHeader 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:
        cm - the column model for the table
    • Method Detail

      • setTable

        public void setTable​(javax.swing.JTable table)
        Sets the table associated with this header. Also adds a TableModelListener to the table's TableModel to allow resizing of columns if data of table changed.
        Overrides:
        setTable in class javax.swing.table.JTableHeader
        Parameters:
        table - the new table
      • setAutoResizingEnabled

        public void setAutoResizingEnabled​(boolean autoResizingEnabled)
        Sets whether columns are resized on table model events.
        Parameters:
        autoResizingEnabled - true if columns are resized automatically
        See Also:
        getAutoResizingEnabled()
      • getAutoResizingEnabled

        public boolean getAutoResizingEnabled()
        Returns true if auto resizing is enabled.
        Returns:
        the autoResizingEnabled property
        See Also:
        setAutoResizingEnabled(boolean)
      • setIncludeHeaderWidth

        public void setIncludeHeaderWidth​(boolean includeHeaderWidth)
        Sets whether the header's width are included on calculation
        Parameters:
        includeHeaderWidth - true if the headers are included
        See Also:
        getIncludeHeaderWidth()
      • getIncludeHeaderWidth

        public boolean getIncludeHeaderWidth()
        Returns true, if the header's width are
        Returns:
        the setIncludeHeaderWidth property
        See Also:
        setIncludeHeaderWidth(boolean)
      • resizeColumn

        public void resizeColumn​(javax.swing.table.TableColumn column)
        Resizes the given column to fit all its content.
        Parameters:
        column - The TableColumn to resize.
      • resizeAllColumns

        public void resizeAllColumns()
        Resizes all columns to fit all their content.
      • setColumnWidths

        public void setColumnWidths​(javax.swing.table.TableColumn column,
                                    int preferredWidth,
                                    int minWidth,
                                    int maxWidth)
        Sets preferred width, the minimum and maximum width for a given column. The minimum and maximum widths are the bounds for the preferred width. If the preferred width is not in the region of minmum and maximum, it will be adjusted. Also the user cannot resize columns out of this bounds by moving the edge or double clicking it. If a width is set to -1, no change is made to this value.
        Parameters:
        column - The TableColumn to change.
        preferredWidth - The preferred width of the column.
        minWidth - The minimum width of the column.
        maxWidth - The maximum width of the column.
        See Also:
        setAllColumnWidths(int preferredWidth, int minWidth, int maxWidth)
      • tableChanged

        public void tableChanged​(javax.swing.event.TableModelEvent e)
        Listen for table model events from tablemodel. If new data is inserted, only determine the width of new cell and adjust column width, if necessary. If data is deleted or updated, call resizeAllColumns().
        Specified by:
        tableChanged in interface javax.swing.event.TableModelListener
        Parameters:
        e - The TableModelEvent