Skip to content

Instantly share code, notes, and snippets.

@james-d
Created November 27, 2013 14:57
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/7677049 to your computer and use it in GitHub Desktop.
Save james-d/7677049 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.converter.DoubleStringConverter;
import javafx.util.converter.IntegerStringConverter;
public class BoundTableColumnExample extends Application {
@Override
public void start(Stage primaryStage) {
final BorderPane root = new BorderPane();
final TableView<Product> table = new TableView<>();
table.setEditable(true);
final TableColumn<Product, String> nameCol = createTableColumn(String.class, "Name", true);
final TableColumn<Product, Double> priceCol = createTableColumn(Double.class, "Price", true);
final TableColumn<Product, Integer> quantityCol = createTableColumn(Integer.class, "Quantity", true);
final TableColumn<Product, Double> totalPriceCol = createTableColumn(Double.class, "Total Price", false);
nameCol.setCellFactory(TextFieldTableCell.<Product>forTableColumn());
priceCol.setCellFactory(TextFieldTableCell.<Product, Double>forTableColumn(new DoubleStringConverter()));
quantityCol.setCellFactory(TextFieldTableCell.<Product, Integer>forTableColumn(new IntegerStringConverter()));
table.getColumns().addAll(Arrays.asList(nameCol, priceCol, quantityCol, totalPriceCol));
table.getItems().addAll(createData());
root.setCenter(table);
final Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private List<Product> createData() {
return Arrays.asList(
new Product("Widget"),
new Product("Gadget"),
new Product("Doohickey")
);
}
private <T> TableColumn<Product, T> createTableColumn(Class<T> clz, String title, boolean editable) {
TableColumn<Product, T> col = new TableColumn<>(title);
col.setCellValueFactory(new PropertyValueFactory<Product, T>(makePropertyName(title)));
col.setEditable(editable);
col.setMinWidth(120);
return col ;
}
private String makePropertyName(String title) {
boolean first = true ;
StringBuilder propName = new StringBuilder();
for (String word : title.split("\\s")) {
if (first) {
propName.append(word.toLowerCase());
first = false ;
} else {
propName.append(Character.toUpperCase(word.charAt(0)));
if (word.length() > 1) {
propName.append(word.substring(1));
}
}
}
return propName.toString();
}
public static void main(String[] args) {
launch(args);
}
public static class Product {
private final StringProperty name ;
private final IntegerProperty quantity ;
private final DoubleProperty price ;
private final ReadOnlyDoubleWrapper totalPrice ;
public Product(String name) {
this.name = new SimpleStringProperty(this, "name", name);
this.quantity = new SimpleIntegerProperty(this, "quantity", 0);
this.price = new SimpleDoubleProperty(this, "price", 0);
this.totalPrice = new ReadOnlyDoubleWrapper(this, "totalPrice");
// bind total price to product of price and quantity:
totalPrice.bind(price.multiply(quantity));
}
public final String getName() {
return name.get();
}
public final void setName(String name) {
this.name.set(name);
}
public final StringProperty nameProperty() {
return name ;
}
public final int getQuantity() {
return quantity.get();
}
public final void setQuantity(int quantity) {
this.quantity.set(quantity);
}
public final IntegerProperty quantityProperty() {
return quantity ;
}
public final double getPrice() {
return price.get();
}
public final void setPrice(double price) {
this.price.set(price);
}
public final DoubleProperty priceProperty() {
return price ;
}
public final double getTotalPrice() {
return totalPrice.get();
}
public final ReadOnlyDoubleProperty totalPriceProperty() {
return totalPrice.getReadOnlyProperty();
}
@Override
public String toString() {
return String.format("%s:\t%.2f\t%d\t%.2f", getName(), getPrice(), getQuantity(), getTotalPrice());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment