如何从TableView javafx中的列中获取Cell值? [英] How fetch a Cell value from a column in TableView javafx?
问题描述
我正在开发一个基本的计费应用程序,对javafx来说是全新的。
使用添加和删除表格中的产品并查找总费用来完成。主要问题是在删除行后扣除总费用。
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.
上面的屏幕截图显示了值从该特定列中取出(例如,来自adams的88)并存储到变量中。
我发现很难获取单元格的值并将其存储在变量中,这样我就可以从总成本中减少它。
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.
这是我的控制器代码。
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);
}
}
这是我的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>
推荐答案
您没有从单元格获取数据,你从模型中得到它:
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...
这篇关于如何从TableView javafx中的列中获取Cell值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!