JavaFX How to get all values and sum of specific column from Table View by button?

Jaroslaw :

I have a problem with adding functionality to my program. I'm using JavaFX and Table View. I don't know how can I implement a method witch will get data from specific column.

I try to implement solution from this said: JavaFX How to get all values of one column from TableView?

But I don't understand exactly how should I send initialized TableColumn class to method totalPaidSaving

And: How to get selected TableCell in JavaFX TableView

I Also found a couple of different solutions using getSelectionModel().getSelectedCells() but in this case I need to use button and some sort of code witch will get data from specific column.

Fragment of MainControl class. Place where the method is called :

addButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        Operation operation = new Operation();
        operation.addRecord(dateTextField, distanceTextField, lpgAmountTextField, lpgPriceTextField,
                    petrolAmountTextField, petrolPriceTextField, paidLabel, savingLabel, gasEfficiencyLabel,
                    contentTable, totalSavingsLabel);

        operation.totalPaidSaving(contentTable, totalSavingsLabel);
    }
});

A fragment of the configuration of my TableView also in the MainControl class.

@SuppressWarnings("unchecked")
public void configurateTable() {

    TableColumn<GasRecords, String> savingColumn = new TableColumn<GasRecords, String>(SAVING_COLUMN);
    savingColumn.setCellValueFactory(new PropertyValueFactory<>("saving"));

    contentTable.getColumns().addAll(dateColumn, distanceColumn, lpgAmountColumn, lpgPriceColumn, petAmountColumn,
            petPriceColumn, paidColumn, savingColumn, gasEfficiencyColumn);
}

A fragment of Operation class:

public void totalPaidSaving(TableView<GasRecords> cT, Label tSL) {

    String totalSavingValue = "0";

    //the code that calculates the sum of the values ​​of a particular column

    tSL.setText(String.format("%.2f zł", totalSavingValue));
}

I know that using String instead of double is bad but for now I would like to solve it in this form.

Link of visualization of me application and example of column i want to sum. https://zapodaj.net/d8fc7ece3f629.jpg.html

My goal is to, after press the "Add" button application shows me the sum of all savings rows from saving column in specific label. Rest of application is working.

Jamith :

In this ways you can calculate the values of a column. you can get cell value by the index of the column. you have to use contentTable.getColumns().get(7).getCellObservableValue(i).getValue() get the cell value of that column Savings.

int totalSavingValue=0;
       for (int i= 0;i<tblDispatchHistory.getItems().size();i++){
            total = total+Integer.valueOf(String.valueOf(contentTable.getColumns().get(7).getCellObservableValue(i).getValue()));
           }
 System.out.println(totalSavingValue);
 tSL.setText(totalSavingValue);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109015&siteId=1