JavaFX .How to make a line of summation(total row) in the bottom of the table?

后端 未结 3 2055
名媛妹妹
名媛妹妹 2021-01-01 06:06

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

3条回答
  •  清歌不尽
    2021-01-01 06:25

    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 LineItems (my model class), so my TransformationList implementation looks like:

    public class LineItemListWithTotal extends TransformationList {
    
        private final TotalLine totalLine ;
    
        protected LineItemListWithTotal(
                ObservableList source) {
            super(source);
            totalLine = new TotalLine(source);
        }
    
        @Override
        protected void sourceChanged(Change 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 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 total = new ReadOnlyObjectWrapper<>();
    
        public TotalLine(ObservableList items) {
            super("Total", null, null);
    
            total.bind(Bindings.createObjectBinding(() -> 
                    items.stream().collect(Collectors.summingDouble(LineItem::getTotal)), items));
        }
    
        @Override
        public ReadOnlyObjectProperty 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

    enter image description here

提交回复
热议问题