java - 如何从 TableView javafx 中的列中获取单元格值?

标签 java javafx

我正在开发一个基本的计费应用程序,对 javafx 来说是全新的。 已完成从表中添加和删除产品,并找出总成本。主要问题是删除一行后推导总成本。

 Value to be fetched

上面的屏幕截图显示应该从该特定列中获取值(例如来自 adams 的 88)并将其存储到变量中。 我发现很难获取单元格的值并将其存储在变量中,以便我可以减少总成本。

这是我的 Controller 代码。

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>

最佳答案

您不是从单元格获取数据,而是从模型获取数据:

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...

关于java - 如何从 TableView javafx 中的列中获取单元格值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34511142/

相关文章:

java - 如何使用 Spring 属性配置 Spring 查找方法

macos - javafxpackager 在 Mac OS X 上错误地定位应用程序文件夹

JavaFX - 创建带有图像的自定义按钮

java - 如何创建空媒体对象以便不引发异常?

java - 我的代码出现 mysql 内存泄漏?

java - 无法使用 Apache 从 Android 向 Django Web 服务器发出 http 请求

JavaFX:双向绑定(bind)的初始值

JavaFX-11 与 VSCode

java - Selenium :NoSuchElementException

java - 如何计算字符串数组中的重复项?