- All Implemented Interfaces:
Serializable
,CellEditor
,TableCellEditor
,TreeCellEditor
- Direct Known Subclasses:
ITextCellEditor
Typically the text that is entered must follow some syntactic
rules. If not, a red square will appear around a cell and the
user can either re-edit the text or hit the escape key to
revert to the previous value. The class
ITextCellEditor
, by contrast, brings up a dialog box
when it detects an input error.
Note: for Ubuntu or Pop!_OS openjdk packages such as
openjdk-11-... version 11.0.17+8-1ubuntu2~22.04 or
openjdk-17-... version 17.0.5+8-2ubuntu1~22.04 there is a
bug in which, when editing text in a JTable, neither the
text nor a caret is visible. This occurs when a
DefaultCellEditor
is created with the constructor
DefaultCellEditor(JTextField)
explicitly used, and the look and feel set to the system
look and feel. The reference implementations, however, work as
expected. To see if this bug has been fixed, compile
TableTest.java from the source code's tests/swing directory and run
java -classpath=... TableTest --systemUI (the argument sets the
look and feel to the system look and feel).
The initial motivation for {#link TextCellEditor} was to
work around this bug, but the constructors providing functions
for formatting and parsing provide extra capabilities. Because
JTable alone did not exhibit this bug and includes a (non public)
cell editor names GenericEditor, the code for that class was used
as a model for TextCellEditor
.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.DefaultCellEditor
DefaultCellEditor.EditorDelegate
-
Field Summary
Fields inherited from class javax.swing.DefaultCellEditor
clickCountToStart, delegate, editorComponent
Fields inherited from class javax.swing.AbstractCellEditor
changeEvent, listenerList
-
Constructor Summary
ConstructorsConstructorDescriptionTextCellEditor
(Class<T> clasz) Constructor.Constructor providing formatters and parsers.TextCellEditor
(Class<T> clasz, JTextField tf) Constructor given a text field..TextCellEditor
(Class<T> clasz, JTextField tf, Function<T, String> formatter, Function<String, T> parser) Constructor providing a text field, formatters and parsers. -
Method Summary
Modifier and TypeMethodDescriptiongetTableCellEditorComponent
(JTable table, Object value, boolean isSelected, int row, int column) getTreeCellEditorComponent
(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) boolean
Methods inherited from class javax.swing.DefaultCellEditor
cancelCellEditing, getClickCountToStart, getComponent, isCellEditable, setClickCountToStart, shouldSelectCell
Methods inherited from class javax.swing.AbstractCellEditor
addCellEditorListener, fireEditingCanceled, fireEditingStopped, getCellEditorListeners, removeCellEditorListener
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.swing.CellEditor
addCellEditorListener, removeCellEditorListener
-
Constructor Details
-
TextCellEditor
Constructor.With this constructor, the implentation assumes that clasz has a single-argument public constructor that takes a string as its argument, and that constructor will be used to create an instance of the class provided by the type parameter T.
- Parameters:
clasz
- the class of the object being edited; null if the class for a table's column should be used.
-
TextCellEditor
Constructor given a text field..With this constructor, the implentation assumes that clasz has a single-argument public constructor that takes a string as its argument, and that constructor will be used to create an instance of the class provided by the type parameter T.
- Parameters:
clasz
- the class of the object being edited; null if the class for a table's column should be used.tf
- the text field used to store values that are being edited
-
TextCellEditor
Constructor providing formatters and parsers. The parameter clasz should be the class object for the type parameter T.- Parameters:
clasz
- the class of the object being editedformatter
- a function that maps objects of type T to stringsparser
- a function that maps strings to objects of type T
-
TextCellEditor
public TextCellEditor(Class<T> clasz, JTextField tf, Function<T, String> formatter, Function<String, T> parser) Constructor providing a text field, formatters and parsers. The parameter clasz should be the class object for the type parameter T.- Parameters:
clasz
- the class of the object being editedtf
- the text field used to store values that are being editedformatter
- a function that maps objects of type T to stringsparser
- a function that maps strings to objects of type T
-
-
Method Details
-
stopCellEditing
public boolean stopCellEditing()- Specified by:
stopCellEditing
in interfaceCellEditor
- Overrides:
stopCellEditing
in classDefaultCellEditor
-
getTableCellEditorComponent
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) - Specified by:
getTableCellEditorComponent
in interfaceTableCellEditor
- Overrides:
getTableCellEditorComponent
in classDefaultCellEditor
-
getTreeCellEditorComponent
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) - Specified by:
getTreeCellEditorComponent
in interfaceTreeCellEditor
- Overrides:
getTreeCellEditorComponent
in classDefaultCellEditor
-
getCellEditorValue
- Specified by:
getCellEditorValue
in interfaceCellEditor
- Overrides:
getCellEditorValue
in classDefaultCellEditor
-