基本上我正在尝试输入2个日期,这些日期将传递给另一个控制器中的2个不同的字符串,以便它更改条形图中的文本以便它运行报告。
选择2个日期时的代码:
public void okayBtnPressed() throws SQLException, IOException{
con = handler.getConnection();
int count = 0;
String query1 = "SELECT * FROM assigned WHERE Date BETWEEN ? AND ?";
pst = con.prepareStatement(query1);
pst.setString(1, datePickerFrom.getValue().toString());
pst.setString(2, datePickerTo.getValue().toString());
ResultSet rs = pst.executeQuery();
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/bownhrmain1/reports/assignedReport.fxml"));
Parent root = myLoader.load();
AssignedReportController assignedController = myLoader.getController();
while(rs.next()){
count ++;
}
if(count == 0){
// no results
} else {
System.out.println(""+count);
System.out.println(datePickerFrom.getValue().toString());
System.out.println(datePickerTo.getValue().toString());
date1 = datePickerFrom.getEditor().getText();
date2 = datePickerTo.getEditor().getText();
assignedController.date1 = date1;
assignedController.date2 = date2;
assignedController.amount = count;
Stage window = (Stage)(okayBtn.getScene()).getWindow();
Scene scene = new Scene(root);
window.setScene(scene);
}
}
所有system.out行都会打印正确的日期,在这种情况下,它会打印正确的日期,并在1上返回一个行值。
此报告显示在两个不同日期之间分配给员工的项目数。
这是将数据应用于条形图的代码:(这是assignedController)
public String date1 = "";
public String date2 = "";
public String totalDate = "";
public int amount;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
totalDate = date1+" - "+date2;
System.out.println(totalDate);
System.out.println(""+amount);
XYChart.Series set1 = new XYChart.Series<>();
set1.getData().add(new XYChart.Data(totalDate, amount));
barChart1.getData().addAll(set1);
}
我没有错误,但在assignedController中int的数量是= 0,两个字符串都不返回任何内容。
这是条形图页面的fxml:
<AnchorPane id="AnchorPane" prefHeight="473.0" prefWidth="737.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="reports.AssignedReportController">
<children>
<BarChart fx:id="barChart1" layoutX="119.0" layoutY="44.0" title="Stock Assigned">
<xAxis>
<CategoryAxis fx:id="monthAxis" label="Month" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="amountAxis" label="Amount Sold" side="LEFT" />
</yAxis>
</BarChart>
</children>
</AnchorPane>
答案 0 :(得分:1)
在执行initialize()
期间调用控制器中的FXMLLoader.load()
方法,这当然是在您在控制器中设置值之前。您需要在控制器上调用更新数据的方法。 (这是为什么你永远不应该使用公共字段的一个很好的例子 - 它允许更改那些字段而不控制可能依赖于它们的其他字段(在这种情况下是条形图)会发生什么。)
你需要像
这样的东西public class AssignedReportController {
@FXML
private BarChart<String, Number> barchart1 ;
private XYChart.Series<String, Number> set1 ;
public void initialize() {
set1 = new XYChart.Series<>();
barChart1.getData().add(set1);
}
public void setDates(String date1, String date2, int amount) {
String totalDate = date1 + " - " + date2 ;
set1.getData().add(new XYChart.Data<>(totalDate, amount));
}
// ...
}
然后在您的按钮处理程序中,您只需要
date1 = datePickerFrom.getEditor().getText();
date2 = datePickerTo.getEditor().getText();
assignedController.setDates(date1, date2, count);
顺便说一句,您应该考虑在SQL中使用count函数,并且应该关闭不再使用的JDBC资源:
public void okayBtnPressed() throws SQLException, IOException{
con = handler.getConnection();
String query1 = "SELECT COUNT(*) FROM assigned WHERE Date BETWEEN ? AND ?";
pst = con.prepareStatement(query1);
pst.setString(1, datePickerFrom.getValue().toString());
pst.setString(2, datePickerTo.getValue().toString());
ResultSet rs = pst.executeQuery();
int count = rs.next().getInt(1);
rs.close();
pst.close();
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/bownhrmain1/reports/assignedReport.fxml"));
Parent root = myLoader.load();
AssignedReportController assignedController = myLoader.getController();
if(count == 0){
// no results
} else {
System.out.println(""+count);
System.out.println(datePickerFrom.getValue().toString());
System.out.println(datePickerTo.getValue().toString());
date1 = datePickerFrom.getEditor().getText();
date2 = datePickerTo.getEditor().getText();
assignedController.setDates(date1, date2, count);
Stage window = (Stage)(okayBtn.getScene()).getWindow();
Scene scene = new Scene(root);
window.setScene(scene);
}
}