I am working on a basic billing application, totally new to javafx. Am done with adding and deleting the products from the table and also finding out the total cost .The main issue is to deduce the Total Cost after deleting a row.
The above screenshot shows up the value from that particular column should be fetched (eg.88 from adams) and stored into a variable. I find it difficult to fetch the value of a cell and store it in a variable so that i can reduce it from the total cost.
Here is my Controller Code.
package tabletdma;
import Data.Data;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
/**
*
* @author amir
*/
public class InterfaceController implements Initializable {
@FXML
TableView<Data> tableID;
@FXML
TableColumn<Data, String> iName;
@FXML
TableColumn<Data, Double> iQty;
@FXML
TableColumn<Data, Double> iPrice;
@FXML
TableColumn<Data, Double> iTotal;
@FXML
TextField name;
@FXML
TextField quantity;
@FXML
TextField price;
@FXML
Button add;
@FXML
Button delete;
@FXML
private Label Sample;
@FXML
Text txt;
Double FinalCost = 0.0;
String output1;
//Array Initiazlization
final ObservableList<Data> data= FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
iName.setCellValueFactory(new PropertyValueFactory<>("rName"));
iQty.setCellValueFactory(new PropertyValueFactory<>("rQty"));
iPrice.setCellValueFactory(new PropertyValueFactory<>("rPrice"));
iTotal.setCellValueFactory(new PropertyValueFactory<>("rTotal"));
tableID.setItems(data);
}
@FXML
public void onAddItem(ActionEvent event){
Double qty = Double.parseDouble(quantity.getText());
Double unitprice = Double.parseDouble(price.getText());
double Total;
//Total Price Processing
Total = qty * unitprice;
//FinalCost Processing
FinalCost = FinalCost + Total;
output1 = " " + FinalCost;
//Debudding
System.out.println(output1);
Data entry = new Data(name.getText(), qty, unitprice ,Total);
//Insert Data in the Table
data.add(entry);
Sample.setText(output1);
//Clear all the Text field;
ClearForm();
}
/**
*
* @param event1
*/
public void onDeleteItem(ActionEvent event1){
ObservableList<Data> productSelected, allProducts;
allProducts = tableID.getItems();
productSelected = tableID.getSelectionModel().getSelectedItems();
/*
TablePosition pos = tableID.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
// Item here is the table view type:
Data item = tableID.getItems().get(row);
TableColumn col = pos.getTableColumn();
// this gives the value in the selected cell:
double data1 = (double) col.getCellObservableValue(item).getValue();
FinalCost = FinalCost - data1;
System.out.println(output1);
//System.out.println(CurrentPrice);
*/
productSelected.forEach(allProducts::remove);
}
private void ClearForm() {
name.clear();
quantity.clear();
price.clear();
}
}
Here is the Data processing code from another package.
package Data;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
// name qty unit price total
public class Data {
private final SimpleStringProperty rName;
private final SimpleDoubleProperty rQty;
private final SimpleDoubleProperty rPrice;
private final SimpleDoubleProperty rTotal;
public Data(String sName, Double sQty, Double sPrice, Double sTotal ){
this.rName = new SimpleStringProperty(sName);
this.rQty = new SimpleDoubleProperty(sQty);
this.rPrice = new SimpleDoubleProperty(sPrice);
this.rTotal = new SimpleDoubleProperty(sTotal);
}
//Name
public String getRName(){
return rName.get();
}
public void setRName(String v){
rName.set(v);
}
//Quantity
public Double getRQty(){
return rQty.get();
}
public void setRQty(Double v){
rQty.set(v);
}
//Unit Price
public Double getRPrice(){
return rPrice.get();
}
public void setRPrice(Double v){
rPrice.set(v);
}
//Total Cost
//Unit Price
public Double getRTotal(){
return rTotal.get();
}
public void setRTotal(Double v){
rTotal.set(v);
}
}
Here is my FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="635.0" prefWidth="873.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tabletdma.InterfaceController">
<children>
<Label fx:id="Sample" layoutX="754.0" layoutY="543.0" minHeight="16" minWidth="69" prefHeight="33.0" prefWidth="86.0" text=" 00">
<font>
<Font size="20.0" />
</font></Label>
<Button fx:id="add" layoutX="474.0" layoutY="46.0" onAction="#onAddItem" text="Add" />
<Button fx:id="delete" layoutX="528.0" layoutY="46.0" onAction="#onDeleteItem" text="Delete" />
<TextField fx:id="name" layoutX="27.0" layoutY="46.0" promptText="Name " />
<TextField fx:id="price" layoutX="286.0" layoutY="46.0" promptText="Price" />
<TextField fx:id="quantity" layoutX="204.0" layoutY="46.0" prefHeight="27.0" prefWidth="73.0" promptText="Qty" />
<Text layoutX="656.0" layoutY="567.0" strokeType="OUTSIDE" strokeWidth="0.0" text=" Total Rs." wrappingWidth="141.0">
<font>
<Font size="20.0" />
</font>
</Text>
<TableView fx:id="tableID" layoutX="27.0" layoutY="87.0" prefHeight="442.0" prefWidth="805.0">
<columns>
<TableColumn fx:id="iName" prefWidth="424.0" text="Name " />
<TableColumn fx:id="iQty" minWidth="0.0" prefWidth="93.0" text="Quantity" />
<TableColumn fx:id="iPrice" prefWidth="140.0" text="Price" />
<TableColumn fx:id="iTotal" prefWidth="147.0" text="Total" />
</columns>
</TableView>
</children>
</AnchorPane>
You don't get the data from the cell, you get it from the model:
double totalCostOfSelectedItems = 0 ;
for (Data product : productSelected) {
totalCostOfSelectedItems = totalCostOfSelectedItems + product.getRTotal();
}
finalCost = finalCost - totalCostOfSelectedItems() ;
allProducts.removeAll(productSelected);
And of course you could always just recompute the total after removal instead, which would only be a performance issue if you had a huge number of products in the table:
allProducts.removeAll(productSelected) ;
finalCost = allProducts.stream().collect(Collectors.summingDouble(Data::getRTotal));
// update label...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.