Java examples for JavaFX:TextField
create JavaFX TextField
/*/*from w w w . j a va 2 s . co m*/ * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ //package com.java2s; import javafx.scene.Node; import javafx.scene.control.Cell; import javafx.scene.control.TextField; import javafx.scene.input.KeyCode; import javafx.util.StringConverter; public class Main { static <T> TextField createTextField(final Cell<T> cell, final StringConverter<T> converter) { final TextField textField = new TextField(getItemText(cell, converter)); // Use onAction here rather than onKeyReleased (with check for Enter), // as otherwise we encounter RT-34685 textField.setOnAction(event -> { if (converter == null) { throw new IllegalStateException("Attempting to convert text input into Object, but provided " + "StringConverter is null. Be sure to set a StringConverter " + "in your cell factory."); } cell.commitEdit(converter.fromString(textField.getText())); event.consume(); }); textField.setOnKeyReleased(t -> { if (t.getCode() == KeyCode.ESCAPE) { cell.cancelEdit(); t.consume(); } }); return textField; } private static <T> String getItemText(Cell<T> cell, StringConverter<T> converter) { return converter == null ? cell.getItem() == null ? "" : cell.getItem().toString() : converter.toString(cell.getItem()); } static <T> void cancelEdit(Cell<T> cell, final StringConverter<T> converter, Node graphic) { cell.setText(getItemText(cell, converter)); cell.setGraphic(graphic); } }