Jadex 0.96-beta1

jadex.util.jtable
Class ResizeableTableHeader

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.table.JTableHeader
                  extended by jadex.util.jtable.ResizeableTableHeader
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, TableColumnModelListener, TableModelListener

public class ResizeableTableHeader
extends JTableHeader
implements 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.

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/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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
ResizeableTableHeader()
          Constructs a ResizeableTableHeader with a default TableColumnModel.
ResizeableTableHeader(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
 void resizeAllColumns()
          Resizes all columns to fit all their content.
 void resizeColumn(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(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(JTable table)
          Sets the table associated with this header.
 void tableChanged(TableModelEvent e)
          Listen for table model events from tablemodel.
 
Methods inherited from class javax.swing.table.JTableHeader
columnAdded, columnAtPoint, columnMarginChanged, columnMoved, columnRemoved, columnSelectionChanged, getAccessibleContext, getColumnModel, getDefaultRenderer, getDraggedColumn, getDraggedDistance, getHeaderRect, getReorderingAllowed, getResizingAllowed, getResizingColumn, getTable, getToolTipText, getUI, getUIClassID, getUpdateTableInRealTime, 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, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, 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, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, 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, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ResizeableTableHeader

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


ResizeableTableHeader

public ResizeableTableHeader(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(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 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(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(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(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 TableModelListener
Parameters:
e - The TableModelEvent

Jadex 0.96-beta1

Submit a bug or feature
For further API reference and developer documentation, see the Jadex User Guide and the Jadex Tutorial. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, and working code examples.

Copyright (C) 2002-2007 Lars Braubach, Alexander Pokahr - University of Hamburg. Use is subject to license terms.