Reputation: 849
I have a tableview, in one of his columns i calculate the subTotal: Column "subTotal" = "Qty * price", after calculating the subtotals, i calculate the sum of all subtotals. Maybe using: - a label,So How i can store and retrieve the sum from a label as needed. - Spinner, So How to bind the sum to the Spinner's valueProperty.
Here is the first option using a label:
DoubleBinding doubleBinding = Bindings.createDoubleBinding(() -> tableView.getItems().stream().
collect(Collectors.summingDouble(LineCommand::getSubTotal)), tableView.getItems());
lblTotalHt.textProperty().bind(Bindings.format("%3.2f", doubleBinding));
The problem how i can retreive the stored sum ?
the second option: How to perform this using a spinner
fldTotalHt.valueProperty().....Bind(toTheSum);
Upvotes: 0
Views: 731
Reputation: 209225
For your first question: "How can I retrieve the stored sum?", you can get this from the binding with
double sum = doubleBinding.getValue();
For the second question, you can do
Spinner<Double> spinner = new Spinner(min, max, doubleBinding.getValue());
spinner.getValueFactory().valueProperty().bind(doubleBinding.asObject());
(asObject()
effectively converts the ObservableValue<Number>
to an ObservableValue<Double>
).
But note that binding a spinner's value is pretty pointless. A bound value cannot be set, for obvious reasons, so the user would not be allowed to change the value of the spinner - this means it would have no more functionality than a label. It's really not clear why you would want to display this value in a spinner, since it really doesn't make any sense for the user to be able to change a value which is computed from other values. Why not just use a label?
Upvotes: 1
Reputation: 417
If fldTotalHt
is a Spinner
, then you can access its value property like this
fldTotalHt.getValueFactory().valueProperty().bind(sumProperty)
About a label. I don't quite understand, what you really what. As I understood, you just can't get label's contents. But that's simply label.getText()
and it's strange if you know about properties and bindings, but don't know about that.
Upvotes: 1