javafx中的可编辑表

javafx中的可编辑表,javafx,Javafx,我正在尝试用javafx构建一个计费系统,我可以构建表和所有东西。现在我想更改它,我希望表已经可以编辑了,也就是说,在OneDitCommittee之前,表中的文本字段应该可以编辑。同时,将数据存储在表中也是一个问题。 代码如下所示。在下面的代码中,正在添加行,可以删除行。但我想在单击“新建账单”按钮时使其可编辑。还有任何通过乘以this.rate和this.qty计算价格的方法 public class abc extends Application { private TableVi

我正在尝试用javafx构建一个计费系统,我可以构建表和所有东西。现在我想更改它,我希望表已经可以编辑了,也就是说,在OneDitCommittee之前,表中的文本字段应该可以编辑。同时,将数据存储在表中也是一个问题。 代码如下所示。在下面的代码中,正在添加行,可以删除行。但我想在单击“新建账单”按钮时使其可编辑。还有任何通过乘以this.rate和this.qty计算价格的方法

    public class abc extends Application {
private TableView<Billing> table = new TableView<Billing>();
private final ObservableList<Billing> data = FXCollections.observableArrayList();
final HBox hb = new HBox();
final HBox hb1=new HBox();
final HBox hb2= new HBox();
private IntegerProperty index = new SimpleIntegerProperty();

public static void main(String[] args) {
launch(args); }

// Stage Start method. Whole Stage.
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Billing");
stage.setWidth(550);
stage.setHeight(650);
final Label label = new Label("Billing Trial 2 ");
label.setFont(new Font("Comic Sans", 26));

//Call EditingCell and set true to make it editable
table.setEditable(true);
Callback<TableColumn, TableCell> cellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
return new EditingCell();
}
};

TableColumn srnoCol = new TableColumn("Sr. No. ");
srnoCol.setMinWidth(50);
srnoCol.setCellValueFactory(
new PropertyValueFactory<Billing, String>("srNo"));

srnoCol.setCellFactory(cellFactory);
TableColumn numCol = new TableColumn("Item Code ");
numCol.setMinWidth(50);
numCol.setCellValueFactory(
new PropertyValueFactory<Billing, String>("itemCode"));
numCol.setCellFactory(cellFactory);
numCol.setOnEditCommit(
new EventHandler<CellEditEvent<Billing, String>>() {
@Override
public void handle(CellEditEvent<Billing, String> t) {
((Billing) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setItemCode(t.getNewValue());
}
}
);
TableColumn nameCol = new TableColumn("Item Name ");
nameCol.setMinWidth(100);
nameCol.setCellValueFactory(
new PropertyValueFactory<Billing, String>("itemName"));
nameCol.setCellFactory(cellFactory);
nameCol.setOnEditCommit(
new EventHandler<CellEditEvent<Billing, String>>() {
@Override
public void handle(CellEditEvent<Billing, String> t) {
((Billing) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setItemName(t.getNewValue());
}
}
);

TableColumn qtyCol = new TableColumn("Qty ");
qtyCol.setMinWidth(100);
qtyCol.setCellValueFactory(
new PropertyValueFactory<Billing, String>("itemQty"));
qtyCol.setCellFactory(cellFactory);
qtyCol.setOnEditCommit(
new EventHandler<CellEditEvent<Billing, String>>() {
@Override
public void handle(CellEditEvent<Billing, String> t) {
((Billing) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setItemQty(t.getNewValue());
}
}
);

TableColumn rateCol = new TableColumn("Item Rate ");
rateCol.setMinWidth(50);
rateCol.setCellValueFactory(
new PropertyValueFactory<Billing, String>("itemRate"));
rateCol.setCellFactory(cellFactory);
rateCol.setOnEditCommit(
new EventHandler<CellEditEvent<Billing, String>>() {
@Override
public void handle(CellEditEvent<Billing, String> t) {
((Billing) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setItemRate(t.getNewValue());
}
}
);
TableColumn priceCol = new TableColumn("Item Price ");
priceCol.setMinWidth(50);
priceCol.setCellValueFactory(
new PropertyValueFactory<Billing, String>("itemPrice"));
priceCol.setCellFactory(cellFactory);
priceCol.setOnEditCommit(
new EventHandler<CellEditEvent<Billing, String>>() {
@Override
public void handle(CellEditEvent<Billing, String> t) {
((Billing) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setItemPrice(t.getNewValue());
}
}
);
table.setItems(data);

//indexing of elements for deleting function.
table.getSelectionModel().selectedItemProperty().addListener(newChangeListener<Object>() {
@Override
public void changed(ObservableValue<?>observable, Object oldvalue, Object newValue){
index.set(data.indexOf(newValue));
System.out.println("index: "+data.indexOf(newValue));
}
});

//Deleting
final Button deleteButton=new Button("Delete");
deleteButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent de){
int i = index.get();
if (i>-1){
data.remove(i);
table.getSelectionModel().clearSelection();
}
}
});

TableColumn amount = new TableColumn("Amount");
amount.getColumns().addAll(rateCol, priceCol);
table.setItems(data);
table.getColumns().addAll(srnoCol, numCol, nameCol, qtyCol, amount );



//add bill
final Button addButton = new Button("New Bill");
addButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent ae)
{
EditingCell ec = new EditingCell();
ec.getString();
ec.createTextField();
table.setEditable(true);
Callback<TableColumn, TableCell>  cellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
return new EditingCell();
}
};
ec.startEdit();
ec.cancelEdit();

data.add(new Billing(null,null,null,null,null,null));
}
});

hb.getChildren().addAll( addButton, deleteButton);
hb.setAlignment(Pos.BASELINE_LEFT);
hb.setSpacing(16);
final Label label2 = new Label();
label2.setAlignment(Pos.BASELINE_RIGHT);

final VBox vbox = new VBox();
vbox.setSpacing(15);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label,hb2, hb,  table,hb1);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}

//Class Billing
public  static class Billing {
private final SimpleStringProperty itemSrNo;
private final SimpleStringProperty itemCode;
private final SimpleStringProperty itemName;
private final SimpleStringProperty itemQty;
private final SimpleStringProperty itemRate;
private final SimpleStringProperty itemPrice;
private Billing(String iSrNo, String iCode, String iName, String iQty, String iRate,String iPrice) 
{
this.itemSrNo = new SimpleStringProperty(iSrNo);
this.itemCode = new SimpleStringProperty(iCode);
this.itemName = new SimpleStringProperty(iName);
this.itemPrice = new SimpleStringProperty(iPrice);
this.itemQty = new SimpleStringProperty(iQty);
this.itemRate = new SimpleStringProperty(iRate);
}
public String getItemSrNo() {
return itemSrNo.get();
}
public void setItemSrNo(String iSrNo) {
itemSrNo.set(iSrNo);
}

public String getItemCode() {
return itemCode.get();
}
public void setItemCode(String iCode) {
itemCode.set(iCode);
}
public String getItemName() {
return itemName.get();
}
public void setItemName(String iName) {
itemName.set(iName);
}
public String getItemQty() {
return itemQty.get();
}
public void setItemQty(String iQty) {
itemQty.set(iQty);
}
public String getItemPrice() {
return itemPrice.get();
}
public void setItemPrice(String iPrice) {
itemPrice.set(iPrice);
}
public String getItemRate() {
return itemRate.get();
}
public void setItemRate(String iRate) {
itemRate.set(iRate);
}
}

//CellEditing
public  class EditingCell extends TableCell<Billing, String> {
private TextField textField;
public EditingCell() {
}
@Override
public void startEdit() {
if (!isEmpty()) {
super.startEdit();
createTextField();
setText(null);
setGraphic(textField);
textField.selectAll();
}
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText((String) getItem());
setGraphic(null);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
}
else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
}
else {
setText(getString());
setGraphic(null);
}
}
}
private void createTextField() {
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
textField.focusedProperty().addListener(new ChangeListener<Boolean>(){
@Override
public void changed(ObservableValue<? extends Boolean> arg0, 
Boolean arg1, Boolean arg2) {
if (!arg2) {
commitEdit(textField.getText());
}
}
});
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
}

// calculation of Qty x Rate.
class Calculate {

public String sum(int iQty, int iRate)
{
 int sum =  iQty*iRate;
 String s =""+sum;
 return s;
}
}
公共类abc扩展应用程序{
private TableView table=new TableView();
私有最终ObservableList数据=FXCollections.observableArrayList();
最终HBox hb=新HBox();
最终HBox hb1=新HBox();
最终HBox hb2=新HBox();
私有IntegerProperty索引=新的SimpleIntegerProperty();
公共静态void main(字符串[]args){
启动(args);}
//阶段开始法。整个阶段。
@凌驾
公众假期开始(阶段){
场景=新场景(新组());
阶段。设置标题(“账单”);
舞台布景宽度(550);
舞台设置高度(650);
最终标签=新标签(“计费试验2”);
label.setFont(新字体(“Comic Sans”,26));
//调用EditingCell并设置为true以使其可编辑
table.setEditable(true);
回叫手机工厂=
新回调函数(){
公共TableCell调用(TableP列){
返回新的EditingCell();
}
};
TableColumn srnoCol=新的TableColumn(“Sr.编号”);
srnoCol.setMinWidth(50);
srnoCol.setCellValueFactory(
新物业价值工厂(“srNo”);
srnoCol.setCellFactory(cellFactory);
TableColumn numCol=新的TableColumn(“项目代码”);
单位设置最小宽度(50);
numCol.setCellValueFactory(
新的PropertyValueFactory(“项目代码”);
numCol.setCellFactory(cellFactory);
numCol.setOnEditCommit(
新的EventHandler(){
@凌驾
公共无效句柄(CellEditEvent t){
((计费)t.getTableView().getItems().get(
t、 getTablePosition().getRow())
).setItemCode(t.getNewValue());
}
}
);
TableColumn Name col=新的TableColumn(“项目名称”);
名称列设置最小宽度(100);
nameCol.setCellValueFactory(
新财产价值工厂(“项目名称”);
名称:setCellFactory(cellFactory);
nameCol.setOnEditCommit(
新的EventHandler(){
@凌驾
公共无效句柄(CellEditEvent t){
((计费)t.getTableView().getItems().get(
t、 getTablePosition().getRow())
).setItemName(t.getNewValue());
}
}
);
TableColumn qtyCol=新的TableColumn(“数量”);
qtyCol.setMinWidth(100);
qtyCol.setCellValueFactory(
新财产价值工厂(“项目数量”);
qtyCol.setCellFactory(cellFactory);
qtyCol.setOnEditCommit(
新的EventHandler(){
@凌驾
公共无效句柄(CellEditEvent t){
((计费)t.getTableView().getItems().get(
t、 getTablePosition().getRow())
).setItemQty(t.getNewValue());
}
}
);
TableColumn rateCol=新的TableColumn(“项目费率”);
额定设定最小宽度(50);
rateCol.setCellValueFactory(
新财产价值工厂(“项目费率”);
rateCol.setCellFactory(cellFactory);
rateCol.setOnEditCommit(
新的EventHandler(){
@凌驾
公共无效句柄(CellEditEvent t){
((计费)t.getTableView().getItems().get(
t、 getTablePosition().getRow())
).setItemRate(t.getNewValue());
}
}
);
TableColumn priceCol=新的TableColumn(“项目价格”);
价格设置最小宽度(50);
priceCol.setCellValueFactory(
新物业价值工厂(“项目价格”);
priceCol.setCellFactory(cellFactory);
priceCol.setOnEditCommit(
新的EventHandler(){
@凌驾
公共无效句柄(CellEditEvent t){
((计费)t.getTableView().getItems().get(
t、 getTablePosition().getRow())
).setItemPrice(t.getNewValue());
}
}
);
表2.设置项目(数据);
//用于删除函数的元素索引。
table.getSelectionModel().SelectEditeProperty().addListener(newChangeListener()){
@凌驾
公共无效已更改(ObservableValueobservable、Object oldvalue、Object newValue){
index.set(data.indexOf(newValue));
System.out.println(“索引:+data.indexOf(newValue));
}
});
//删除
最终按钮deleteButton=新按钮(“删除”);
deleteButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent de){
int i=index.get();
如果(i>-1){
数据删除(i);
table.getSelectionModel().clearSelection();
}
}
});
TableColumn金额=新的TableColumn(“金额”);
amount.getColumns().addAll(rateCol,priceCol);
表2.设置项目(数据);
table.getColumns().addAll(srnoCol、numCol、nameCol、qtyCol、amount);
//添加账单
最终按钮addButton=新按钮(“新账单”);
addButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ae)
{
EditingCell ec=新的EditingCell();
ec.getString();
ec.createTextField();
table.setEditable(true);
回叫手机工厂=
新回调函数(){
公共TableCell调用(TableP列){
返回新的EditingCell();
}
};
ec.启动IT();
ec.cancelEdit();
添加(新账单(空,空,空,空,空,空,空));
}
});
hb.getChildren().addAll(addButton,deleteButton);
hb.设置校准(左位置基线);
hb.1(16);
最终标签label2=新标签();
标签2.设置对齐(右位置基线);
最终VBox VBox=新的VBox();
vbox.setspace(15);
设置填充(新的插入(10,0,0,10));
vbox.getChildren().addAll(标签,hb2,hb,表格,hb1);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
//分类计费
公共静态类计费{
私人最终SimpleStringProperty项目编号;
私有最终SimpleStringProperty项目代码;
私有最终SimpleStringProperty itemName;
私有最终SimpleStringProperty项目数量;
私人最终简单租赁财产项目费率;
私人物业最终价格;
私人计费(字符串iSrNo、字符串iCode、字符串iName、字符串iQty、字符串iRate、字符串iPrice)
{
this.itemSrNo=新的SimpleStringProperty(iSrNo);
this.itemCode=新的SimpleStringProperty(iCode);
this.itemName=新的SimpleStringProperty(iName);
this.itemPrice=新的SimpleStringProperty(iPrice);
this.itemQty=新的SimpleStringProperty(iQty);
this.itemRate=新的SimpleStringProperty(iRate);
}
公共字符串getItemSrNo(){
返回itemSrNo.get();
}
public void setItemSrNo(字符串iSrNo){
没有。