public class EditableTableHeader extends SortableTableHeader implements CellEditorListener
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.JTableHeader.AccessibleJTableHeader
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
protected TableCellEditor |
_cellEditor |
protected TableCellEditor |
_defaultEditor |
protected int |
_editingColumn |
protected Component |
_editorComp |
int |
HEADER_ROW |
PROPERTY_SHOW_SORT_ARROW
_cellStyleList
PROPERTY_DRAGGED_COLUMN, PROPERTY_DRAGGED_DISTANCE, PROPERTY_RESIZING_COLUMN
columnModel, draggedColumn, draggedDistance, reorderingAllowed, resizingAllowed, resizingColumn, table, updateTableInRealTime
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
EditableTableHeader(JTable table)
The constructor that takes JTable
|
EditableTableHeader(TableColumnModel columnModel)
Creates an
EditableTableHeader . |
Modifier and Type | Method and Description |
---|---|
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.
|
createSortableTableHeaderCellDecorator, createSortIcon, getSortableTableHeaderCellDecorator, isShowSortArrow, paintComponent, paintSortArrow, paintSortArrows, setShowSortArrow
addCellDecorator, addCellDecorator, addCellStyleCustomizer, clearCellStyleCustomizers, collectCellStyles, createToolTip, customizeRendererComponent, getActualHeaderY, getCellDecorators, getCellStyleAt, getCellStyleCustomizers, getDefaultCursor, mergeCellStyles, originalColumnAtPoint, releaseRendererComponent, removeCellDecorator, removeCellStyleCustomizer, setCursor, setDefaultRenderer, updateUI
getPreferredSize, getToolTipText, setDraggedDistance, setResizingColumn
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
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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, 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
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
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, 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, transferFocusBackward, transferFocusUpCycle
public final int HEADER_ROW
protected transient int _editingColumn
protected transient TableCellEditor _cellEditor
protected transient Component _editorComp
protected TableCellEditor _defaultEditor
public EditableTableHeader(JTable table)
table
- the tablepublic EditableTableHeader(TableColumnModel columnModel)
EditableTableHeader
.columnModel
- the column model.public String getUIDelegateClassID()
getUIDelegateClassID
in class SortableTableHeader
public String getActualUIClassID()
SortableTableHeader
getActualUIClassID
in class SortableTableHeader
public boolean isRolloverEnabled()
public void setRolloverEnabled(boolean rolloverEnabled)
rolloverEnabled
- true or false.public void resizeAndRepaint()
revalidate
followed by
repaint
. EditableTableHeader
overrides this method to stop cell editing first.resizeAndRepaint
in class JTableHeader
public void stopEditing()
public void cancelEditing()
protected TableCellEditor createDefaultEditor()
public void setDefaultEditor(TableCellEditor defaultEditor)
defaultEditor
- the default editorpublic TableCellEditor getDefaultEditor()
public boolean editCellAt(int columnIndex)
columnIndex
- the column index to be edited.public void setDraggedColumn(TableColumn column)
setDraggedColumn
in class DraggingTableHeader
public boolean editCellAt(int columnIndex, EventObject e)
columnIndex
- the column index to be edited.e
- the event that triggers the editing.protected void customizeEditorComponent(Component component, int rowIndex, int columnIndex)
component
- the editor componentrowIndex
- the row indexcolumnIndex
- the column indexpublic boolean isCellEditable(int columnIndex)
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.columnIndex
- the column whose value is to be queriedpublic TableCellEditor getCellEditor(int columnIndex)
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()
.columnIndex
- the column index.protected void setCellEditor(TableCellEditor newEditor)
newEditor
- the new cell editor that is using.public Component prepareEditor(TableCellEditor editor, int columnIndex)
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.editor
- the TableCellEditor
to set upcolumnIndex
- the column of the cell to edit, where 0 is the first columnComponent
being editedpublic TableCellEditor getCellEditor()
null
if the table header is not currently editing.TableCellEditor
that does the editing, or null
if the table header is not currently
editing.getCellEditor(int)
public Component getEditorComponent()
public void setEditingColumn(int aColumn)
editingColumn
variable.aColumn
- the column of the cell to be editedpublic int getEditingColumn()
public void removeEditor()
public boolean isEditing()
public void editingStopped(ChangeEvent e)
editingStopped
in interface CellEditorListener
public void editingCanceled(ChangeEvent e)
editingCanceled
in interface CellEditorListener
public void removeNotify()
unconfigureEnclosingScrollPane
method.removeNotify
in class JComponent
public boolean isClickToStartEditing()
public void setClickToStartEditing(boolean clickToStartEditing)
protected boolean isAutoRequestFocus()