How do I bind a BO currency (LongProperty) to a Javafx editable TreeTableView? Using Databinding, TextFormatter and other javaFX Stuff.
For a normal TextField I found this solution: Editiing Currency with a TextField
Bo:
import java.util.Random;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
public class SimpleBo {
//a simple LongProperty to store the currency without fractional digits (56,81 € would be 5681)
private LongProperty currencyLong = new SimpleLongProperty();
public SimpleBo() {
setCurrencyLong(new Random().nextLong());
}
public final LongProperty currencyLongProperty() {
return this.currencyLong;
}
public final long getCurrencyLong() {
return this.currencyLongProperty().get();
}
public final void setCurrencyLong(final long currencyLong) {
this.currencyLongProperty().set(currencyLong);
}
}
Application:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.stage.Stage;
public class BindingExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createTreeTableView());
primaryStage.setScene(scene);
primaryStage.show();
}
private static TreeTableView<SimpleBo> createTreeTableView() {
TreeTableView<SimpleBo> treeTableView = new TreeTableView<>();
// Create column (Data type of Long).
TreeTableColumn<SimpleBo, Number> currencyColumn = new TreeTableColumn<>("Currency");
//Bind Values
currencyColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("currencyLong"));
//Set Cell Factory
currencyColumn.setCellFactory( param -> new TextFieldTreeTableCell<>());
// Add columns to TreeTable.
treeTableView.getColumns().add(currencyColumn);
SimpleBo firstBo = new SimpleBo();
SimpleBo secondBo = new SimpleBo();
SimpleBo thirdBo = new SimpleBo();
// Root Item
TreeItem<SimpleBo> itemRoot = new TreeItem<>(null);
TreeItem<SimpleBo> itemFirst = new TreeItem<>(firstBo);
TreeItem<SimpleBo> itemSecond = new TreeItem<>(secondBo);
TreeItem<SimpleBo> itemThird = new TreeItem<>(thirdBo);
itemRoot.getChildren().addAll(itemFirst, itemSecond, itemThird);
// Set root Item for Tree
treeTableView.setRoot(itemRoot);
treeTableView.setShowRoot(false);
treeTableView.setEditable(true);
return treeTableView;
}
}
The goal should be:
- Bo with a LongProperty (currency Value in cents)
- editable TreeTable, in the Users known format (optinal leading minus, thousands separator, decimal separator, currency symbol, and no other chars possible)
- BiDirectionalBinding between Bo and TreeTableColumn.