我正在开发一个基本的结算应用程序,对javafx来说是全新的。 完成添加和删除表中的产品,并查找总费用。主要问题是在删除行后推断总费用。
上面的屏幕截图显示了应从特定列中获取的值(例如,来自adams的88)并存储到变量中。 我发现很难获取单元格的值并将其存储在变量中,以便我可以从总成本中减少它。
这是我的控制器代码。
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();
}
}
以下是其他软件包的数据处理代码。
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);
}
}
这是我的FXML文件
<?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>
答案 0 :(得分:1)
您无法从单元格中获取数据,您可以从模型中获取数据:
IRepository<Role, EmptyEnum> repo = UnitOfWork.GetRepository<Role, EmptyEnum>();
IDbTransaction transaction= UnitOfWork.BeginTransaction();
foreach (Role ob in objROles)
{
repo.Add(ob, transaction);
}
UnitOfWork.Commit();
当然,您可以随时重新计算删除后的总数,如果表中有大量产品,这只会是性能问题:
double totalCostOfSelectedItems = 0 ;
for (Data product : productSelected) {
totalCostOfSelectedItems = totalCostOfSelectedItems + product.getRTotal();
}
finalCost = finalCost - totalCostOfSelectedItems() ;
allProducts.removeAll(productSelected);