net.logAnalyzer.utils.gui
Class ResizableTableHeader

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.table.JTableHeader
                  extended bynet.logAnalyzer.utils.gui.ResizableTableHeader
All Implemented Interfaces:
javax.accessibility.Accessible, java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.swing.event.TableColumnModelListener, javax.swing.event.TableModelListener

public class ResizableTableHeader
extends javax.swing.table.JTableHeader
implements javax.swing.event.TableModelListener

This subclass of JTableHeader extends a JTableHeader 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.

Note that you have to set the tableheader's columnmodel to the columnmodel of the table. See example for usage:

 JTable table;
 ResizeableTableHeader tableHeader;
 
 // create new table
 table = new JTable();
 // create new ResizableTableHeader with the table's ColumnModel
 tableHeader = new ResizeableTableHeader(table.getColumnModel());
 // set the new header for the table
 table.setTableHeader(tableHeader);
 
Or for the one line fans
 // create new table
 JTable table = new JTable();
 table.setTableHeader(new ResizeableTableHeader(table.getColumnModel()));
 

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.table.JTableHeader
javax.swing.table.JTableHeader.AccessibleJTableHeader
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.table.JTableHeader
columnModel, draggedColumn, draggedDistance, reorderingAllowed, resizingAllowed, resizingColumn, table, updateTableInRealTime
 
Fields inherited from class javax.swing.JComponent
accessibleContext, 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
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
ResizableTableHeader()
          Constructs a ResizeableTableHeader with a default TableColumnModel.
ResizableTableHeader(javax.swing.table.TableColumnModel cm)
          Constructs a ResizeableTableHeader which is initialized with cm as the column model.
 
Method Summary
 boolean getAutoResizingEnabled()
          Returns true if auto resizing is enabled.
 boolean getIncludeHeaderWidth()
          Returns true, if the header's width are.
 java.lang.String getToolTipText(java.awt.event.MouseEvent event)
           
 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, getReorderingAllowed, getResizingAllowed, getResizingColumn, getTable, 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, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, 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, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, 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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ResizableTableHeader

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


ResizableTableHeader

public ResizableTableHeader(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.

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)

setAllColumnWidths

public void setAllColumnWidths(int preferredWidth,
                               int minWidth,
                               int maxWidth)
Sets preferred width, the minimum and maximum width for all columns. See setColumnWidth() for further details.

Parameters:
preferredWidth - The preferred width of the column.
minWidth - The minimum width of the column.
maxWidth - The maximum width of the column.
See Also:
setColumnWidths(javax.swing.table.TableColumn column,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

getToolTipText

public java.lang.String getToolTipText(java.awt.event.MouseEvent event)


Copyright © 2006 null. All Rights Reserved.