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

后端 未结 3 2031
名媛妹妹
名媛妹妹 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<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

    enter image description here

    0 讨论(0)
  • 2021-01-01 06:26

    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.

    • Use a "TableView"
    • 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".

    • Set a listener on all of them except the last one. (You can do this in a loop)
      • Each listener should call your sum function. This function should calculate the last element of your list = the sum of the other element in the list
      • The "TableView" will automatically update it self.
    0 讨论(0)
  • 2021-01-01 06:30

    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:

    enter image description here

    0 讨论(0)
提交回复
热议问题