Class TextCellEditor<T>

All Implemented Interfaces:
Serializable, CellEditor, TableCellEditor, TreeCellEditor
Direct Known Subclasses:
ITextCellEditor

public class TextCellEditor<T> extends DefaultCellEditor
Cell editor for objects that can be represented textually. The type parameter generally should be the type of a JTable column, but may be a super type of the column's type if the class provided in a constructor is null.

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:
  • Constructor Details

    • TextCellEditor

      public TextCellEditor(Class<T> clasz)
      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

      public TextCellEditor(Class<T> clasz, JTextField tf)
      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

      public TextCellEditor(Class<T> clasz, Function<T,String> formatter, Function<String,T> parser)
      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 edited
      formatter - a function that maps objects of type T to strings
      parser - 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 edited
      tf - the text field used to store values that are being edited
      formatter - a function that maps objects of type T to strings
      parser - a function that maps strings to objects of type T
  • Method Details