如何在tableview中动态添加数据?

时间:2018-02-21 07:14:33

标签: javafx

这是在fxml文件中指定的colid,我不知道怎么回事。如果按下添加按钮,则输入的数据不会显示在表格视图中。添加了一些空白行。我想在用户的tableview中添加数据。

@FXML
private TableColumn<AddItemDetails, String> colofiice_name;

@FXML
private TableColumn<AddItemDetails, String> colref_name;

@FXML
private TableColumn<AddItemDetails, String> colch_item_name;

@FXML
private TableColumn<AddItemDetails, String> colch_item_code;

@FXML
private TableColumn<AddItemDetails, String> colch_unit;

@FXML
private TableColumn<AddItemDetails, String> colch_qty;

@FXML
private TableColumn<AddItemDetails, String> colch_rec_by;

@FXML
private TableColumn<AddItemDetails, String> colch_desig;

@FXML
private TableColumn<AddItemDetails, String> colch_addr;

@FXML
private TableColumn<AddItemDetails, String> colch_remark;

@FXML
private TableColumn<AddItemDetails, String> colch_no;

@FXML
private TableColumn<AddItemDetails, String> colch_action;



private JFXCheckBox CB;

我在控制器中编写了这段代码

public void AddTableView(ActionEvent event)throws SQLException{

    AddItemDetails additem = new AddItemDetails();
    additem.setOfficeName(select_office.getValue());
    additem.setRefNo(challan_select_item.getValue());
    additem.setItemName(txt_ref_no.getText());
    additem.setItemCode(txt_item_code.getText());
    additem.setItemUnit(txt_unit.getText());
    additem.setItemQty(txt_qty.getText());
    additem.setReceiveBY(txt_rec_by.getText());
    additem.setDesignation(txt_desig.getText());
    additem.setChNo(txt_chllan.getText());
    additem.setAddress(txt_addr.getText());
    additem.setRemarks(txt_remark.getText());
    additem.setAction(CB);


    tableview2.getItems().addAll(additem);




    }catch(Exception e){
        e.printStackTrace();
    }

这是我的AddItemDetails类

package inventory_system_app;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXComboBox;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;


public class AddItemDetails {

private  String office_name;
private  String ref_no;
private  String I_name;
private  String I_code;
private  String I_unit;
private  String I_qty;
private  String rec_by;
private  String desig;
private  String ch_no;
private  String address;
private  String ch_remarks;
private JFXCheckBox Action;


public AddItemDetails(String office_name,String ref_no,String I_name,String  
I_code,String I_unit, String I_qty,String rec_by,String desig,String 
ch_no,String address, String ch_remarks, String value){

    this.office_name = office_name;
    this.ref_no = ref_no;
    this.I_name = I_name;
    this.I_code = I_code;
    this.I_unit = I_unit;
    this.I_qty = I_qty;
    this.rec_by = rec_by;
    this.desig = desig;
    this.ch_no = ch_no;
    this.address = address;
    this.ch_remarks = ch_remarks;
    this.Action = new JFXCheckBox();

}

AddItemDetails() {

}



public String getOfficeName(){ 
    return office_name;
}

public String getRefNo(){ 
    return ref_no;
}

public String getItemName(){ 
    return I_name;
}

public String getItemCode(){ 
    return I_code;
}

public String getItemUnit(){ 
    return I_unit;
}

public String getItemQty(){ 
    return I_qty;
}

public String getReceiveBY(){ 
    return rec_by;
}

public String getDesignation(){ 
    return desig;
}

public String getChNo(){ 
    return ch_no;
}

public String getAddress(){ 
    return address;
}

public String getRemarks(){ 
    return ch_remarks;
  }

   public JFXCheckBox getAction() {
    return Action;
   }


   public void setOfficeName(String value){
    this.office_name= value;
   }

     public void setRefNo(String value){
    this.ref_no=value;
    }

    public void setItemName(String value){
    this.I_name=value;
    }

    public void setItemCode(String value){
    this.I_code=value;
   }

   public void setItemUnit(String value){
    this.I_unit=value;
}

    public void setItemQty(String value){
    this.I_qty=value;
}

    public void setReceiveBY(String value){
    this.rec_by=value;
}

    public void setDesignation(String value){
    this.desig=value;
}

    public void setChNo(String value){
    this.ch_no=value;
}

  public void setAddress(String value){
    this.address=value;
}

 public void setRemarks(String value){
    this.ch_remarks=value;
}

public void setAction(JFXCheckBox Action) {
    this.Action = Action;
} 


}

1 个答案:

答案 0 :(得分:0)

使代码正常工作的步骤:

1。  在AddItemDetails替换您希望在String表格中显示的每个StringProperty字段中:

    public class AddItemDetails {

    private  StringProperty office_name;
    private  StringProperty ref_no;
    private  StringProperty I_name;
    .
    .
    .


    public AddItemDetails(String office_name,String ref_no,String I_name,...){

    this.office_name = new SimpleStringProperty(office_name);
    this.ref_no = new SimpleStringProperty(ref_no);
    this.I_name = new SimpleStringProperty(I_name);
    .
    .
    .
    //  + getters 
  1. 为每列设置cellValueFactory - s:

    colofiice_name.setCellValueFactory(data -> data.getValue().office_nameProperty());
    colref_name.setCellValueFactory(data -> data.getValue().colref_nameProperty());
    item_name.setCellValueFactory(data -> data.getValue().item_nameProperty());
    
  2. 向tableView添加新项目:

    tableView.getItems().add(new AddItemDetails("Office","Ref","I_name",...));
    
  3. 完成这些步骤后,它应该可以正常工作。

    修改

    你可以使用 ObservableList<AddItemDetails> myData = FXCollecctions.observableArraylist();

    用于存储数据,然后:

    tableView.setItems(myData);
    

    然后你可以在这个列表中添加和删除元素,它也会在tableview中更新数据。就像:

    mydata.add(new AddItemDetails(...));
    myData.remove(...);