将变量从javaFX控制器传递到其他控制器

时间:2017-12-06 23:29:24

标签: java javafx fxml

我刚开始编程几个月前上学,而我目前正在JavaFX中制作行李注册系统。我的前3个屏幕是您在文本字段中填写所需信息的地方。在这3个屏幕之后,下一个屏幕必须包含用户在前3个屏幕中填写的所有内容的摘要。在看了很多关于如何在控制器之间传递变量的教程后,我无法让它工作。

这是我的初始控制器

   @FXML
private void handleButtonAction1(ActionEvent event) throws IOException, Exception {

    labelNumber = label_number.getText();
    flightNumber = flight_number.getText();
    destination = destination_.getText();

    if (nullOrEmpty(labelNumber) || nullOrEmpty(flightNumber) || nullOrEmpty(destination)) {

        warningLabel.setTextFill(Color.RED);
        warningLabel.setText("Please fill in every field");

    } else {

        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

        //Set the second screen fxml document on stage.
        SetStage setStage1 = new SetStage(stage, "FXML1Document.fxml");

        //Pass variables onto FXML3document.fxml
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXML3Document.fxml"));
        FXML3DocumentController FXML3DocumentController = loader.getController();
        FXML3DocumentController.SetTextLabelInfo(labelNumber, flightNumber, destination);
        MyJDBC myJDBC = new MyJDBC("fysdatabasefinal");

        String query = "INSERT INTO luggage(label_number,flight_number,destination) VALUES ('" + (labelNumber) + "','" + (flightNumber) + "','" + (destination) + "')";

        myJDBC.executeUpdateQuery(query);

        myJDBC.close();

    }

}

按下按钮,它应该带我到下一个控制器(FXML1Document.fxml)。它也应该将给定的变量传递给FXML3Document.fxml

这是FXML3DocumentController

public class FXML3DocumentController implements Initializable {

@FXML
private Label first_name;
@FXML
private Label last_name;
@FXML
private Label email_address;
@FXML
private Label phone_number;
@FXML
private Label address_;
@FXML
private Label postal_code;
@FXML
private Label state_;
@FXML
private Label city_;
@FXML
private Label choice_country;
@FXML
private Label type_;
@FXML
private Label brand_;
@FXML
private Label color_;
@FXML
private Label label_number;
@FXML
private Label flight_number;
@FXML
private Label destination_;
@FXML
private Label special_char;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

}
@FXML
public void SetTextClientInfo(String first_name, String last_name, String email_address, String phone_number, String address_,
        String postal_code, String state_, String city_, String choice_country) {

    this.first_name.setText(first_name);
    this.last_name.setText(last_name);
    this.email_address.setText(email_address);
    this.phone_number.setText(phone_number);
    this.address_.setText(address_);
    this.postal_code.setText(postal_code);
    this.state_.setText(state_);
    this.city_.setText(city_);
    this.choice_country.setText(choice_country);


}
@FXML
public void SetTextLabelInfo(String label_number, String flight_number, String destination_) {
    this.label_number.setText(label_number);
    this.flight_number.setText(flight_number);
    this.destination_.setText(destination_);

}
@FXML
public void SetTextLuggageInfo(String type_,
        String brand_, String special_char) {
    this.type_.setText(type_);
    this.brand_.setText(brand_);
    this.special_char.setText(special_char);

}

我创建了3个函数,我在3个控制器中的每个控制器中调用它们,获取文本字段值并更改上述控制器中的标签。

非常感谢任何帮助。

0 个答案:

没有答案