< prev index next >

modules/javafx.controls/src/main/java/javafx/scene/control/cell/TextFieldTableCell.java

Print this page
rev 10463 : 8089514: [TableView, TreeView, ListView, TreeTableView] Clicking outside of the edited cell, node, or entry should commit the value


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control.cell;
  27 
  28 import javafx.beans.property.ObjectProperty;
  29 import javafx.beans.property.SimpleObjectProperty;
  30 import javafx.scene.control.*;
  31 import javafx.util.Callback;
  32 import javafx.util.StringConverter;
  33 import javafx.util.converter.DefaultStringConverter;
  34 


  35 /**
  36  * A class containing a {@link TableCell} implementation that draws a
  37  * {@link TextField} node inside the cell.
  38  *
  39  * <p>By default, the TextFieldTableCell is rendered as a {@link Label} when not
  40  * being edited, and as a TextField when in editing mode. The TextField will, by
  41  * default, stretch to fill the entire table cell.
  42  *
  43  * @param <S> The type of the TableView generic type
  44  * @param <T> The type of the elements contained within the TableColumn.
  45  * @since JavaFX 2.2
  46  */
  47 public class TextFieldTableCell<S,T> extends TableCell<S,T> {
  48 
  49     /***************************************************************************
  50      *                                                                         *
  51      * Static cell factories                                                   *
  52      *                                                                         *
  53      **************************************************************************/
  54 


  71     /**
  72      * Provides a {@link TextField} that allows editing of the cell content when
  73      * the cell is double-clicked, or when
  74      * {@link TableView#edit(int, javafx.scene.control.TableColumn) } is called.
  75      * This method will work  on any {@link TableColumn} instance, regardless of
  76      * its generic type. However, to enable this, a {@link StringConverter} must
  77      * be provided that will convert the given String (from what the user typed
  78      * in) into an instance of type T. This item will then be passed along to the
  79      * {@link TableColumn#onEditCommitProperty()} callback.
  80      *
  81      * @param <S> The type of the TableView generic type
  82      * @param <T> The type of the elements contained within the TableColumn
  83      * @param converter A {@link StringConverter} that can convert the given String
  84      *      (from what the user typed in) into an instance of type T.
  85      * @return A {@link Callback} that can be inserted into the
  86      *      {@link TableColumn#cellFactoryProperty() cell factory property} of a
  87      *      TableColumn, that enables textual editing of the content.
  88      */
  89     public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn(
  90             final StringConverter<T> converter) {
  91         return list -> new TextFieldTableCell<S,T>(converter);
  92     }
  93 
  94 
  95     /***************************************************************************
  96      *                                                                         *
  97      * Fields                                                                  *
  98      *                                                                         *
  99      **************************************************************************/
 100 
 101     private TextField textField;
 102 
 103 
 104 
 105     /***************************************************************************
 106      *                                                                         *
 107      * Constructors                                                            *
 108      *                                                                         *
 109      **************************************************************************/
 110 
 111     /**


 193         if (isEditing()) {
 194             if (textField == null) {
 195                 textField = CellUtils.createTextField(this, getConverter());
 196             }
 197 
 198             CellUtils.startEdit(this, getConverter(), null, null, textField);
 199         }
 200     }
 201 
 202     /** {@inheritDoc} */
 203     @Override public void cancelEdit() {
 204         super.cancelEdit();
 205         CellUtils.cancelEdit(this, getConverter(), null);
 206     }
 207 
 208     /** {@inheritDoc} */
 209     @Override public void updateItem(T item, boolean empty) {
 210         super.updateItem(item, empty);
 211         CellUtils.updateItem(this, getConverter(), null, null, textField);
 212     }






 213 }


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control.cell;
  27 
  28 import javafx.beans.property.ObjectProperty;
  29 import javafx.beans.property.SimpleObjectProperty;
  30 import javafx.scene.control.*;
  31 import javafx.util.Callback;
  32 import javafx.util.StringConverter;
  33 import javafx.util.converter.DefaultStringConverter;
  34 
  35 import java.util.Optional;
  36 
  37 /**
  38  * A class containing a {@link TableCell} implementation that draws a
  39  * {@link TextField} node inside the cell.
  40  *
  41  * <p>By default, the TextFieldTableCell is rendered as a {@link Label} when not
  42  * being edited, and as a TextField when in editing mode. The TextField will, by
  43  * default, stretch to fill the entire table cell.
  44  *
  45  * @param <S> The type of the TableView generic type
  46  * @param <T> The type of the elements contained within the TableColumn.
  47  * @since JavaFX 2.2
  48  */
  49 public class TextFieldTableCell<S,T> extends TableCell<S,T> {
  50 
  51     /***************************************************************************
  52      *                                                                         *
  53      * Static cell factories                                                   *
  54      *                                                                         *
  55      **************************************************************************/
  56 


  73     /**
  74      * Provides a {@link TextField} that allows editing of the cell content when
  75      * the cell is double-clicked, or when
  76      * {@link TableView#edit(int, javafx.scene.control.TableColumn) } is called.
  77      * This method will work  on any {@link TableColumn} instance, regardless of
  78      * its generic type. However, to enable this, a {@link StringConverter} must
  79      * be provided that will convert the given String (from what the user typed
  80      * in) into an instance of type T. This item will then be passed along to the
  81      * {@link TableColumn#onEditCommitProperty()} callback.
  82      *
  83      * @param <S> The type of the TableView generic type
  84      * @param <T> The type of the elements contained within the TableColumn
  85      * @param converter A {@link StringConverter} that can convert the given String
  86      *      (from what the user typed in) into an instance of type T.
  87      * @return A {@link Callback} that can be inserted into the
  88      *      {@link TableColumn#cellFactoryProperty() cell factory property} of a
  89      *      TableColumn, that enables textual editing of the content.
  90      */
  91     public static <S,T> Callback<TableColumn<S,T>, TableCell<S,T>> forTableColumn(
  92             final StringConverter<T> converter) {
  93         return list -> new TextFieldTableCell<>(converter);
  94     }
  95 
  96 
  97     /***************************************************************************
  98      *                                                                         *
  99      * Fields                                                                  *
 100      *                                                                         *
 101      **************************************************************************/
 102 
 103     private TextField textField;
 104 
 105 
 106 
 107     /***************************************************************************
 108      *                                                                         *
 109      * Constructors                                                            *
 110      *                                                                         *
 111      **************************************************************************/
 112 
 113     /**


 195         if (isEditing()) {
 196             if (textField == null) {
 197                 textField = CellUtils.createTextField(this, getConverter());
 198             }
 199 
 200             CellUtils.startEdit(this, getConverter(), null, null, textField);
 201         }
 202     }
 203 
 204     /** {@inheritDoc} */
 205     @Override public void cancelEdit() {
 206         super.cancelEdit();
 207         CellUtils.cancelEdit(this, getConverter(), null);
 208     }
 209 
 210     /** {@inheritDoc} */
 211     @Override public void updateItem(T item, boolean empty) {
 212         super.updateItem(item, empty);
 213         CellUtils.updateItem(this, getConverter(), null, null, textField);
 214     }
 215 
 216     /** {@inheritDoc} */
 217     @Override protected Optional<T> getEditorValue() {
 218         StringConverter<T> converter = getConverter();
 219         return converter == null ? Optional.empty() : Optional.of(converter.fromString(textField.getText()));
 220     }
 221 }
< prev index next >