本文介绍了JavaFx TableView未填充所有必需的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库填充JavaFx TableView列。但有些列
如果ObserveableList中有偶数数据(我已经验证过),则总是为空。

I am trying to fill JavaFx TableView Columns from Database. But some columns Always is empty if there is even data in ObserveableList(I have verified it).

表格如下所示:

The Table is look like this:

ViewBillController.java:

ViewBillController.java:

public class ViewBillController implements Initializable{
Common common = new Common();
@FXML
private Button searchInvoice;
@FXML
private Button searchReg;
@FXML
private TextField search;
@FXML
TableColumn<BillData, String> drugColumn;
@FXML
TableColumn<BillData, String> expColumn;
@FXML
TableColumn<BillData, Integer> qtyColumn;
@FXML
TableColumn<BillData, Double> mrpColumn;
@FXML
TableColumn<BillData, Double> amtColumn;
@FXML
TableColumn<BillData, String> saleDateColumn;
@FXML
TableColumn<BillData, Double> discColumn;

@FXML
private TableView<BillData> billDataTableView;
ObservableList<BillData> billsData = FXCollections.observableArrayList();
Connection connection;
PreparedStatement preparedStatement;
ResultSet resultSet;

private String sqlQuery = null;


@Override
public void initialize(URL location, ResourceBundle resources) {
    billDataTableView.getSelectionModel().setCellSelectionEnabled(true);
    searchReg.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            billDataTableView.getItems().clear();
            sqlQuery = "SELECT * from bill WHERE billno like 19";

            tableDataFill(sqlQuery);
        }
    });

    searchInvoice.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            billDataTableView.getItems().clear();
            //String selectedItem = months.getSelectionModel().getSelectedItem() +"-"+years.getSelectionModel().getSelectedItem();
            //sqlQuery = "SELECT name,qty,exp,mrp,disc,rate,date from bill WHERE billno ="+"'"+search.getText()+"'";
            sqlQuery = "SELECT name,qty,exp,mrp,disc,rate,date from bill WHERE billno=19";
            tableDataFill(sqlQuery);

        }
    });

}

public void tableDataFill(String s){

    try {

        connection = SqlConnect.con();
        preparedStatement = connection.prepareStatement(s);
        resultSet = preparedStatement.executeQuery();

        PropertyValueFactory<BillData,String> drugname = new PropertyValueFactory<BillData,String>("name");
        PropertyValueFactory<BillData,Integer> qtycount = new PropertyValueFactory<BillData,Integer>("qty");
        PropertyValueFactory<BillData,String> expdate = new PropertyValueFactory<BillData,String>("exp");
        PropertyValueFactory<BillData,Double> mrp = new PropertyValueFactory<BillData,Double>("mrp");
        PropertyValueFactory<BillData,Double> dsc = new PropertyValueFactory<BillData,Double>("disc");
        PropertyValueFactory<BillData,Double> amt = new PropertyValueFactory<BillData,Double>("rate");
        PropertyValueFactory<BillData,String> salesDate = new PropertyValueFactory<BillData,String>("date");

        drugColumn.setCellValueFactory(drugname);
        qtyColumn.setCellValueFactory(qtycount);
        expColumn.setCellValueFactory(expdate);
        mrpColumn.setCellValueFactory(mrp);
        discColumn.setCellValueFactory(dsc);
        amtColumn.setCellValueFactory(amt);
        saleDateColumn.setCellValueFactory(salesDate);

        while (resultSet.next()){

            String drug = resultSet.getString("name");
            int qtybought = resultSet.getInt("qty");
            String expire = resultSet.getString("exp");
            Double mrp1 = resultSet.getDouble("mrp");
            Double disc = resultSet.getDouble("disc");
            Double totalamt = resultSet.getDouble("rate");
            String getDate = resultSet.getString("date");
            //System.out.println(drug);

            billsData.add(new BillData(drug,qtybought,expire,mrp1,disc,totalamt,getDate));


        }
        billDataTableView.setItems(billsData);

        for(BillData billData:billsData){
            System.out.println(billData.getDrug()+" "+billData.getDisc()+" "+billData.getSaleDate());
        }

    }
    catch (SQLException e){
        common.CustomException(e);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    finally {
        try {
            common.CloseCon();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

}

BillData.java:

BillData.java:

public class BillData {
private final SimpleStringProperty drug;
private final SimpleIntegerProperty qty;
private final SimpleStringProperty exp;
private final SimpleDoubleProperty mrp;
private final SimpleDoubleProperty disc;
private final SimpleDoubleProperty ttlamt;
private final SimpleStringProperty saleDate;

public BillData(String drug, int qty, String exp, Double mrp, Double disc, Double ttlamt, String saleDate) {
    this.drug = new SimpleStringProperty(drug) ;
    this.qty = new SimpleIntegerProperty(qty);
    this.exp = new SimpleStringProperty(exp);
    this.mrp = new SimpleDoubleProperty(mrp);
    this.disc = new SimpleDoubleProperty(disc);
    this.ttlamt = new SimpleDoubleProperty(ttlamt);
    this.saleDate = new SimpleStringProperty(saleDate);
}



public String getDrug() {
    return drug.get();
}

public SimpleStringProperty drugProperty() {
    return drug;
}

public void setDrug(String drug) {
    this.drug.set(drug);
}

public int getQty() {
    return qty.get();
}

public SimpleIntegerProperty qtyProperty() {
    return qty;
}

public void setQty(int qty) {
    this.qty.set(qty);
}

public String getExp() {
    return exp.get();
}

public SimpleStringProperty expProperty() {
    return exp;
}

public void setExp(String exp) {
    this.exp.set(exp);
}

public double getMrp() {
    return mrp.get();
}

public SimpleDoubleProperty mrpProperty() {
    return mrp;
}

public void setMrp(double mrp) {
    this.mrp.set(mrp);
}

public double getTtlamt() {
    return ttlamt.get();
}

public SimpleDoubleProperty ttlamtProperty() {
    return ttlamt;
}

public void setTtlamt(double ttlamt) {
    this.ttlamt.set(ttlamt);
}

public double getDisc() {
    return disc.get();
}

public SimpleDoubleProperty discProperty() {
    return disc;
}

public void setDisc(double disc) {
    this.disc.set(disc);
}

public String getSaleDate() {
    return saleDate.get();
}

public SimpleStringProperty saleDateProperty() {
    return saleDate;
}

public void setSaleDate(String saleDate) {
    this.saleDate.set(saleDate);
}

}

无法找到问题所在!

推荐答案

请您在 ViewBillController中替换以下语句。 tableDataFill(String)

PropertyValueFactory< BillData,String> drugname = new PropertyValueFactory< BillData,String>(name);

此声明:

PropertyValueFactory< BillData,String> drugname = new PropertyValueFactory< BillData,String>(drug);

并查看结果?

这篇关于JavaFx TableView未填充所有必需的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:59