How to make a line of summation in the bottom of the table(TreeTableView - JavaFX or TableView) ? (Sorry for my english) write an example please.
Picture(Totals) for
I would do this by implementing a TransformationList that adds an extra element at the end of the actual data list.
In my example I have a TableView
displaying LineItem
s (my model class), so my TransformationList
implementation looks like:
public class LineItemListWithTotal extends TransformationList<LineItem, LineItem> {
private final TotalLine totalLine ;
protected LineItemListWithTotal(
ObservableList<? extends LineItem> source) {
super(source);
totalLine = new TotalLine(source);
}
@Override
protected void sourceChanged(Change<? extends LineItem> c) {
// no need to modify change:
// indexes generated by the source list will match indexes in this list
fireChange(c);
}
@Override
public int getSourceIndex(int index) {
if (index < getSource().size()) {
return index ;
}
return -1 ;
}
@Override
public LineItem get(int index) {
if (index < getSource().size()) {
return getSource().get(index);
} else if (index == getSource().size()) {
return totalLine ;
} else throw new ArrayIndexOutOfBoundsException(index);
}
@Override
public int size() {
return getSource().size() + 1 ;
}
}
You might find you need a special subclass of your model class in order to make this work easily (in this example I subclassed LineItem
with TotalLine
).
Create your actual data list with an extractor that retrieves the properties you need to observe when calculating the "total" line (i.e. the properties on which that line depends):
ObservableList<LineItem> items = FXCollections.observableArrayList(item ->
new Observable[] {item.totalProperty()});
and then use that to create the TransformationList
and pass the TransformationList
to the control:
table.setItems(new LineItemListWithTotal(items));
I manage the observer for the total line in the special subclass implementing the total:
public class TotalLine extends LineItem {
private final ReadOnlyObjectWrapper<Double> total = new ReadOnlyObjectWrapper<>();
public TotalLine(ObservableList<? extends LineItem> items) {
super("Total", null, null);
total.bind(Bindings.createObjectBinding(() ->
items.stream().collect(Collectors.summingDouble(LineItem::getTotal)), items));
}
@Override
public ReadOnlyObjectProperty<Double> totalProperty() {
return total ;
}
}
This way, when anything is added to or removed from the original data list, or if any of the properties in the extractor for any of the elements change, the total for the total line is recomputed.
Complete example is here
EDIT :Not the answer of the question. I misunderstood the problem.
I don't have an example but I can explain you one way of doing it.
Each column should be a "TableColumn". This "TableColumn" should be related to an "ObsverbableList" with :
YourColumn.setCellValueFactory(
cellData ->CellData.getValue().yourObservableList.yourFloatProperty());
The numbers inside your list should be "FloatProperty".
In JavaFX it's like in Swing: The quickest way to do it is to create a 2nd table.
You can take a look at a summary table sample code on this gist.
Screenshot: