/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tableviewedit; import java.util.concurrent.ExecutorService; import java.util.concurrent.RunnableFuture; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.concurrent.Task; import javafx.concurrent.Worker; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventHandler; import javafx.event.EventType; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.control.TableColumn.CellEditEvent; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author Miernik */ public class TableViewEdit extends Application { class EditingCell extends TableCell { private TextField textField; public EditingCell() { } @Override public void startEdit() { super.startEdit(); if (textField == null) { createTextField(); } setText(null); setGraphic(textField); //textField.selectAll(); Platform.runLater(new Runnable() { @Override public void run() { textField.requestFocus(); } }); } @Override public void cancelEdit() { super.cancelEdit(); setText((String) getItem()); setGraphic(null); } @Override public void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setGraphic(null); } else { if (isEditing()) { if (textField != null) { textField.setText(getString()); } setText(null); setGraphic(textField); } else { setText(getString()); setGraphic(null); } } } private void createTextField() { textField = new TextField(getString()); textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2); textField.setOnKeyReleased(new EventHandler() { @Override public void handle(KeyEvent t) { if (t.getCode() == KeyCode.ENTER) { commitEdit(textField.getText()); } else if (t.getCode() == KeyCode.ESCAPE) { cancelEdit(); } } }); } private String getString() { return getItem() == null ? "" : getItem().toString(); } } public static class Person { private final SimpleStringProperty firstName; private final SimpleStringProperty lastName; private final SimpleStringProperty email; private Person(String fName, String lName, String email) { this.firstName = new SimpleStringProperty(fName); this.lastName = new SimpleStringProperty(lName); this.email = new SimpleStringProperty(email); } public String getFirstName() { return firstName.get(); } public void setFirstName(String fName) { firstName.set(fName); } public String getLastName() { return lastName.get(); } public void setLastName(String fName) { lastName.set(fName); } public String getEmail() { return email.get(); } public void setEmail(String fName) { email.set(fName); } } private TableView table = new TableView(); private final ObservableList data = FXCollections.observableArrayList( new Person("Jacob", "Smith", "jacob.smith@example.com"), new Person("Isabella", "Johnson", "isabella.johnson@example.com"), new Person("Ethan", "Williams", "ethan.williams@example.com"), new Person("Emma", "Jones", "emma.jones@example.com"), new Person("Michael", "Brown", "michael.brown@example.com")); /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Table View Sample"); stage.setWidth(400); stage.setHeight(500); final Label label = new Label("Address Book"); label.setFont(new Font("Arial", 20)); TableColumn firstNameCol = new TableColumn("First Name"); firstNameCol.setCellValueFactory( new PropertyValueFactory("firstName")); TableColumn lastNameCol = new TableColumn("Last Name"); lastNameCol.setCellValueFactory( new PropertyValueFactory("lastName")); TableColumn emailCol = new TableColumn("Email"); emailCol.setMinWidth(200); emailCol.setCellValueFactory( new PropertyValueFactory("email")); //Enabling editing Callback cellFactory = new Callback() { @Override public TableCell call(TableColumn p) { return new EditingCell(); } }; firstNameCol.setCellFactory(cellFactory); lastNameCol.setCellFactory(cellFactory); emailCol.setCellFactory(cellFactory); table.setEditable(true); //Modifying the firstName property firstNameCol.setOnEditCommit(new EventHandler>() { @Override public void handle(CellEditEvent t) { ((Person) t.getTableView().getItems().get( t.getTablePosition().getRow())).setFirstName(t.getNewValue()); } }); //Modifying the lastName property lastNameCol.setOnEditCommit(new EventHandler>() { @Override public void handle(CellEditEvent t) { ((Person) t.getTableView().getItems().get( t.getTablePosition().getRow())).setLastName(t.getNewValue()); } }); //Modifying the email property emailCol.setOnEditCommit(new EventHandler>() { @Override public void handle(CellEditEvent t) { ((Person) t.getTableView().getItems().get( t.getTablePosition().getRow())).setEmail(t.getNewValue()); } }); table.setItems(data); table.getColumns().addAll(firstNameCol, lastNameCol, emailCol); final Button editButton = new Button("Edit"); editButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { table.edit(table.getSelectionModel().getSelectedIndex(), table.getColumns().get(2)); table.requestFocus(); } }); final Button addButton = new Button("Add"); addButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { Person p = new Person("a", "b", "c"); table.getItems().add(p); table.getSelectionModel().select(p); editButton.fire(); } }); final VBox vbox = new VBox(); vbox.setSpacing(5); vbox.getChildren().addAll(label, table, editButton, addButton); vbox.setPadding(new Insets(10, 0, 0, 10)); ((Group) scene.getRoot()).getChildren().addAll(vbox); stage.setScene(scene); stage.show(); } }