Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@james-d
Created April 17, 2015 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james-d/8c190d31d02c10d54de4 to your computer and use it in GitHub Desktop.
Save james-d/8c190d31d02c10d54de4 to your computer and use it in GitHub Desktop.
Beginnings of a TableView example that has a more "spreadsheet-like" editing facility. Not intended to be production code, but demonstrates some ideas.
import java.util.function.Function;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSingleClickEditTest extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Person> table = new TableView<>();
table.setEditable(true);
TableColumn<Person, String> firstNameCol = createCol("First Name", Person::firstNameProperty, 150);
TableColumn<Person, String> lastNameCol = createCol("Last Name", Person::lastNameProperty, 150);
TableColumn<Person, String> emailCol = createCol("Email", Person::emailProperty, 200);
TableColumn<Person, Void> indexCol = new TableColumn<>("");
indexCol.setCellFactory(col -> {
TableCell<Person, Void> cell = new TableCell<>();
cell.textProperty().bind(Bindings.createStringBinding(() -> {
if (cell.getIndex() >= 0 && cell.getIndex() < table.getItems().size() - 1) {
return Integer.toString(cell.getIndex() + 1);
} else if (cell.getIndex() == table.getItems().size() - 1) {
return "*" ;
} else return "" ;
}, cell.indexProperty(), table.getItems()));
return cell ;
});
indexCol.setPrefWidth(32);
table.getItems().addAll(
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")
);
ChangeListener<String> lastPersonTextListener = new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> obs, String oldText, String newText) {
if (oldText.isEmpty() && ! newText.isEmpty()) {
Person lastPerson = table.getItems().get(table.getItems().size() - 1);
lastPerson.firstNameProperty().removeListener(this);
lastPerson.lastNameProperty().removeListener(this);
lastPerson.emailProperty().removeListener(this);
Person newBlankPerson = new Person("", "", "");
newBlankPerson.firstNameProperty().addListener(this);
newBlankPerson.lastNameProperty().addListener(this);
newBlankPerson.emailProperty().addListener(this);
table.getItems().add(newBlankPerson);
}
}
};
Person blankPerson = new Person("", "", "");
blankPerson.firstNameProperty().addListener(lastPersonTextListener);
blankPerson.lastNameProperty().addListener(lastPersonTextListener);
blankPerson.emailProperty().addListener(lastPersonTextListener);
table.getItems().add(blankPerson);
table.getColumns().add(indexCol);
table.getColumns().add(firstNameCol);
table.getColumns().add(lastNameCol);
table.getColumns().add(emailCol);
VBox root = new VBox(15, table);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableColumn<Person, String> createCol(String title,
Function<Person, ObservableValue<String>> mapper, double size) {
TableColumn<Person, String> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue()));
Callback<TableColumn<Person, String>, TableCell<Person, String>> defaultCellFactory
= TextFieldTableCell.forTableColumn();
col.setCellFactory(column -> {
TableCell<Person, String> cell = defaultCellFactory.call(column);
cell.setOnMouseClicked(e -> {
if (! cell.isEditing() && ! cell.isEmpty()) {
cell.getTableView().edit(cell.getIndex(), column);
}
});
return cell ;
});
col.setPrefWidth(size);
return col ;
}
public class Person {
private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
private final StringProperty email = new SimpleStringProperty(this, "email");
public Person(String firstName, String lastName, String email) {
this.firstName.set(firstName);
this.lastName.set(lastName);
this.email.set(email);
}
public final StringProperty firstNameProperty() {
return this.firstName;
}
public final java.lang.String getFirstName() {
return this.firstNameProperty().get();
}
public final void setFirstName(final java.lang.String firstName) {
this.firstNameProperty().set(firstName);
}
public final StringProperty lastNameProperty() {
return this.lastName;
}
public final java.lang.String getLastName() {
return this.lastNameProperty().get();
}
public final void setLastName(final java.lang.String lastName) {
this.lastNameProperty().set(lastName);
}
public final StringProperty emailProperty() {
return this.email;
}
public final java.lang.String getEmail() {
return this.emailProperty().get();
}
public final void setEmail(final java.lang.String email) {
this.emailProperty().set(email);
}
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment