package test.scenegraph.app; import javafx.application.Application; import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextBox; import javafx.scene.layout.VBox; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.util.Callback; public class ShortAppTableView1 extends Application { public static class Person { StringProperty firstName; private Person(String fName) { this.firstName = new StringProperty(fName); } public String getFirstName() { return firstName.get(); } public void setFirstName(String fName) { firstName.set(fName); } public StringProperty firstNameProperty() { if (firstName == null) { firstName = new StringProperty(); } return firstName; } } TableView table = new TableView(); final ObservableList data = FXCollections.observableArrayList( new Person("Jacob"), new Person("Michael")); VBox hb = new VBox(); public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group(), 400, 800); stage.setTitle("Table View Sample"); final Label label = new Label("Address Book"); TableColumn firstNameCol = new TableColumn(); firstNameCol.setCellFactory(new Callback, TableCell>() { public TableCell call(TableColumn p) { return new EditingCell(); } }); firstNameCol.setText("First"); firstNameCol.setProperty("firstName"); table.setItems(data); table.getColumns().addAll(firstNameCol); final VBox vbox = new VBox(); vbox.setSpacing(5); vbox.getChildren().addAll(label, table, hb); ((Group) scene.getRoot()).getChildren().addAll(vbox); stage.setScene(scene); stage.setVisible(true); } class EditingCell extends TableCell { private final Label label; private TextBox textBox = new TextBox(); public EditingCell() { this.label = new Label(); } @Override public void cancelEdit() { hb.getChildren().add(new Text("cancelEdit() item:" + textBox.getText() + "/"+ label.getText())); // super.cancelEdit(); // setNode(label); } @Override public void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (!isEmpty()) { if (textBox != null) { textBox.setText(item); } label.setText(item); setNode(label); } } } }