如何从TableView javafx中的列中获取Cell值?

我正在开发一个基本的计费应用程序,对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 tableID; @FXML TableColumn iName; @FXML TableColumn iQty; @FXML TableColumn iPrice; @FXML TableColumn 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= 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 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文件

           

您无法从单元格获取数据,您可以从模型中获取数据:

 double totalCostOfSelectedItems = 0 ; for (Data product : productSelected) { totalCostOfSelectedItems = totalCostOfSelectedItems + product.getRTotal(); } finalCost = finalCost - totalCostOfSelectedItems() ; allProducts.removeAll(productSelected); 

当然,你总是可以在删除之后重新计算总数,如果你在表中有大量的产品,这只会是性能问题:

 allProducts.removeAll(productSelected) ; finalCost = allProducts.stream().collect(Collectors.summingDouble(Data::getRTotal)); // update label...