JIDE 3.5.15

com.jidesoft.grid
Class EditableTableHeader

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 com.jidesoft.grid.DraggingTableHeader
                      extended by com.jidesoft.grid.CellStyleTableHeader
                          extended by com.jidesoft.grid.SortableTableHeader
                              extended by com.jidesoft.grid.EditableTableHeader
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CellEditorListener, TableColumnModelListener
Direct Known Subclasses:
AutoFilterTableHeader

public class EditableTableHeader
extends SortableTableHeader
implements CellEditorListener

As the name indicates, EditableTableHeader is an editable JTableHeader.

Here is the code to use EditableTableHeader.

 JTable table = new JTable(tableModel);
 EditableTableHeader header = new EditableTableHeader(table.getColumnModel());
 table.setTableHeader(header);
 
Once EditableTableHeader is installed on the table, user can double click on the table header to start editing. Pressing ENTER will commit editing or pressing ESCAPE to cancel editing.

By default, we will use TextFieldCellEditor as the cell editor. You can either override createDefaultEditor() method or call setDefaultEditor(javax.swing.table.TableCellEditor) to set your own cell editor. Please note, if you use your own cell editor, you may need to register ENTER and ESCAPE key to allow stop or cancel cell editing.

By default, all table columns will be editable. The same cell editor will be used for all columns. If you want only some of the columns editable and each one has its own cell editor, you just need to implement EditableColumnTableModel interface in your table model. EditableColumnTableModel has methods to let you decide which column is editable and what cell editor to be used.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.table.JTableHeader
JTableHeader.AccessibleJTableHeader
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  TableCellEditor _cellEditor
           
protected  TableCellEditor _defaultEditor
           
protected  int _editingColumn
           
protected  Component _editorComp
           
 int HEADER_ROW
           
 
Fields inherited from class com.jidesoft.grid.SortableTableHeader
PROPERTY_SHOW_SORT_ARROW
 
Fields inherited from class com.jidesoft.grid.CellStyleTableHeader
_cellStyleList
 
Fields inherited from class com.jidesoft.grid.DraggingTableHeader
PROPERTY_DRAGGED_COLUMN, PROPERTY_DRAGGED_DISTANCE, PROPERTY_RESIZING_COLUMN
 
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
EditableTableHeader(JTable table)
          The constructor that takes JTable
EditableTableHeader(TableColumnModel columnModel)
          Creates an EditableTableHeader.
 
Method Summary
 void cancelEditing()
          Cancels any existing cell editing.
protected  TableCellEditor createDefaultEditor()
          Creates a default header cell editor to be used.
protected  void customizeEditorComponent(Component component, int rowIndex, int columnIndex)
          Customizes the editor component based on the header style.
 boolean editCellAt(int columnIndex)
          Edits the column at the specified column index.
 boolean editCellAt(int columnIndex, EventObject e)
          Edits the column at the specified column index.
 void editingCanceled(ChangeEvent e)
           
 void editingStopped(ChangeEvent e)
           
 String getActualUIClassID()
          Returns a string that specifies the name of the L&F class that renders this component.
 TableCellEditor getCellEditor()
          Returns the active header cell editor, which is null if the table header is not currently editing.
 TableCellEditor getCellEditor(int columnIndex)
          Gets the header cell editor for the specified column index.
 TableCellEditor getDefaultEditor()
          Returns the default editor.
 int getEditingColumn()
          Returns the index of the column that contains the cell currently being edited.
 Component getEditorComponent()
          Returns the component that is handling the editing session.
 String getUIDelegateClassID()
          Returns a string that specifies the name of the UIDelegate class that paints this component.
protected  boolean isAutoRequestFocus()
          If the editor requests focus when starts editing.
 boolean isCellEditable(int columnIndex)
          Returns true if the header cell at index is editable.
 boolean isClickToStartEditing()
           
 boolean isEditing()
          Returns true if a header cell is being edited.
 boolean isRolloverEnabled()
          Checks if rollover is enabled.
 Component prepareEditor(TableCellEditor editor, int columnIndex)
          Prepares the editor by querying the TableColumnModel for the value and selection state of the header cell at columnIndex.
 void removeEditor()
          Discards the editor object and frees the real estate it used for cell rendering.
 void removeNotify()
          Calls the unconfigureEnclosingScrollPane method.
 void resizeAndRepaint()
          Sizes the header and marks it as needing display.
protected  void setCellEditor(TableCellEditor newEditor)
          Sets the currently used cell editor.
 void setClickToStartEditing(boolean clickToStartEditing)
           
 void setDefaultEditor(TableCellEditor defaultEditor)
          Sets the default editor to be used.
 void setDraggedColumn(TableColumn column)
           
 void setEditingColumn(int aColumn)
          Sets the editingColumn variable.
 void setRolloverEnabled(boolean rolloverEnabled)
          Sets rollover flag.
 void stopEditing()
          Stops any existing cell editing.
 
Methods inherited from class com.jidesoft.grid.SortableTableHeader
createSortableTableHeaderCellDecorator, createSortIcon, getSortableTableHeaderCellDecorator, isShowSortArrow, paintComponent, paintSortArrow, paintSortArrows, setShowSortArrow
 
Methods inherited from class com.jidesoft.grid.CellStyleTableHeader
addCellDecorator, addCellDecorator, addCellStyleCustomizer, clearCellStyleCustomizers, collectCellStyles, createToolTip, customizeRendererComponent, getActualHeaderY, getCellDecorators, getCellStyleAt, getCellStyleCustomizers, getDefaultCursor, mergeCellStyles, originalColumnAtPoint, releaseRendererComponent, removeCellDecorator, removeCellStyleCustomizer, setCursor, setDefaultRenderer, updateUI
 
Methods inherited from class com.jidesoft.grid.DraggingTableHeader
getPreferredSize, getToolTipText, setDraggedDistance, setResizingColumn
 
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, setColumnModel, setReorderingAllowed, setResizingAllowed, setTable, setUI, setUpdateTableInRealTime
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, 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, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, 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, 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, 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, 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, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, 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
 

Field Detail

HEADER_ROW

public final int HEADER_ROW
See Also:
Constant Field Values

_editingColumn

protected transient int _editingColumn

_cellEditor

protected transient TableCellEditor _cellEditor

_editorComp

protected transient Component _editorComp

_defaultEditor

protected TableCellEditor _defaultEditor
Constructor Detail

EditableTableHeader

public EditableTableHeader(JTable table)
The constructor that takes JTable

Parameters:
table - the table
Since:
3.1.0

EditableTableHeader

public EditableTableHeader(TableColumnModel columnModel)
Creates an EditableTableHeader.

Parameters:
columnModel - the column model.
Method Detail

getUIDelegateClassID

public String getUIDelegateClassID()
Returns a string that specifies the name of the UIDelegate class that paints this component.

Overrides:
getUIDelegateClassID in class SortableTableHeader
Returns:
the string "TableHeader.editableTableHeaderUIDelegate"
Since:
3.1.0

getActualUIClassID

public String getActualUIClassID()
Description copied from class: SortableTableHeader
Returns a string that specifies the name of the L&F class that renders this component.

Overrides:
getActualUIClassID in class SortableTableHeader
Returns:
the string "SortableTableHeaderUI"

isRolloverEnabled

public boolean isRolloverEnabled()
Checks if rollover is enabled. If enabled, it will automatically start cell editing when mouse is moved over the table header.

Returns:
true or false.

setRolloverEnabled

public void setRolloverEnabled(boolean rolloverEnabled)
Sets rollover flag. By default, it's false. If enabled, it will automatically start cell editing when mouse is moved over the table header.

Parameters:
rolloverEnabled - true or false.

resizeAndRepaint

public void resizeAndRepaint()
Sizes the header and marks it as needing display. Equivalent to revalidate followed by repaint. EditableTableHeader overrides this method to stop cell editing first.

Overrides:
resizeAndRepaint in class JTableHeader

stopEditing

public void stopEditing()
Stops any existing cell editing.


cancelEditing

public void cancelEditing()
Cancels any existing cell editing.


createDefaultEditor

protected TableCellEditor createDefaultEditor()
Creates a default header cell editor to be used.

Returns:
the default table column editor.

setDefaultEditor

public void setDefaultEditor(TableCellEditor defaultEditor)
Sets the default editor to be used.

Parameters:
defaultEditor - the default editor

getDefaultEditor

public TableCellEditor getDefaultEditor()
Returns the default editor.

Returns:
the default editor

editCellAt

public boolean editCellAt(int columnIndex)
Edits the column at the specified column index.

Parameters:
columnIndex - the column index to be edited.
Returns:
true if editing starts. Otherwise false.

setDraggedColumn

public void setDraggedColumn(TableColumn column)
Overrides:
setDraggedColumn in class DraggingTableHeader

editCellAt

public boolean editCellAt(int columnIndex,
                          EventObject e)
Edits the column at the specified column index.

Parameters:
columnIndex - the column index to be edited.
e - the event that triggers the editing.
Returns:
true if editing starts. Otherwise false.

customizeEditorComponent

protected void customizeEditorComponent(Component component,
                                        int rowIndex,
                                        int columnIndex)
Customizes the editor component based on the header style.

Parameters:
component - the editor component
rowIndex - the row index
columnIndex - the column index
Since:
3.2.3

isCellEditable

public boolean isCellEditable(int columnIndex)
Returns true if the header cell at index is editable.

Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.

Parameters:
columnIndex - the column whose value is to be queried
Returns:
true if the cell is editable

getCellEditor

public TableCellEditor getCellEditor(int columnIndex)
Gets the header cell editor for the specified column index. If the table model or any nested table model in case of TableModelWrapper implement EditableColumnTableModel interface, it will use that interface and call EditableColumnTableModel.getColumnHeaderCellEditor(int) to get the cell editor. If null, we will use getDefaultEditor().

Parameters:
columnIndex - the column index.
Returns:
the cell editor.

setCellEditor

protected void setCellEditor(TableCellEditor newEditor)
Sets the currently used cell editor.

Parameters:
newEditor - the new cell editor that is using.

prepareEditor

public Component prepareEditor(TableCellEditor editor,
                               int columnIndex)
Prepares the editor by querying the TableColumnModel for the value and selection state of the header cell at columnIndex.

Note: Throughout the table package, the internal implementations always use this method to prepare editors so that this default behavior can be safely overridden by a subclass.

Parameters:
editor - the TableCellEditor to set up
columnIndex - the column of the cell to edit, where 0 is the first column
Returns:
the Component being edited

getCellEditor

public TableCellEditor getCellEditor()
Returns the active header cell editor, which is null if the table header is not currently editing.

Returns:
the TableCellEditor that does the editing, or null if the table header is not currently editing.
See Also:
getCellEditor(int)

getEditorComponent

public Component getEditorComponent()
Returns the component that is handling the editing session. If nothing is being edited, returns null.

Returns:
Component handling editing session

setEditingColumn

public void setEditingColumn(int aColumn)
Sets the editingColumn variable.

Parameters:
aColumn - the column of the cell to be edited

getEditingColumn

public int getEditingColumn()
Returns the index of the column that contains the cell currently being edited. If nothing is being edited, returns -1.

Returns:
the index of the column that contains the cell currently being edited; returns -1 if nothing being edited

removeEditor

public void removeEditor()
Discards the editor object and frees the real estate it used for cell rendering.


isEditing

public boolean isEditing()
Returns true if a header cell is being edited.

Returns:
true if the table header is editing a cell

editingStopped

public void editingStopped(ChangeEvent e)
Specified by:
editingStopped in interface CellEditorListener

editingCanceled

public void editingCanceled(ChangeEvent e)
Specified by:
editingCanceled in interface CellEditorListener

removeNotify

public void removeNotify()
Calls the unconfigureEnclosingScrollPane method.

Overrides:
removeNotify in class JComponent

isClickToStartEditing

public boolean isClickToStartEditing()

setClickToStartEditing

public void setClickToStartEditing(boolean clickToStartEditing)

isAutoRequestFocus

protected boolean isAutoRequestFocus()
If the editor requests focus when starts editing. Subclass can override to return false. Default is true.

Returns:
true or false.

JIDE 3.5.15