我正在尝试从数据库填充JavaFx TableView列。但有些专栏 如果ObserveableList中有偶数数据,则始终为空(我已经验证过)。
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:
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);
}
}
无法找到问题所在!
答案 0 :(得分:1)
请您替换ViewBillController.tableDataFill(String)
中的以下声明:
PropertyValueFactory<BillData,String> drugname = new PropertyValueFactory<BillData,String>("name");
这句话:
PropertyValueFactory<BillData,String> drugname = new PropertyValueFactory<BillData,String>("drug");
并查看结果?