在javafx对话框操作上编辑父级元素

时间:2015-10-16 11:26:29

标签: javafx javafx-2 scenebuilder

我有一个带有两个按钮的JavaFX应用程序:"编辑"和"保存"和textfield" textField1"。

最初是textfield" textField1"并且"保存"按钮被禁用(使用场景构建器构建fxml,因此可编辑在那里设置为false)。

当用户点击按钮"编辑"登录对话框弹出窗口。如果用户输入了正确的凭据,那么我想隐藏弹出窗口并解锁(可编辑)textfield" textField1"和按钮"保存"这样用户就可以编辑文本字段值并将其保存到数据库中。

保存后我想再次锁定(不可编辑)"保存"按钮和文本字段" textField1"。

这是我的主要应用程序类:

public class Main extends Application {
    ...
    private Stage theStage;
    ...
    public static void main(String[] args) throws IOException {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        //setTheStage(primaryStage);
        try {
            AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("FXMLDocument.fxml"));
            Scene scene = new Scene(page, initWidth, initHeight);
            primaryStage.setScene(scene);

            currentPane(scene, page);

            primaryStage.show();

        } catch (IOException ex) {
            log.log(Level.INFO, null, ex);            
        }
    }

这是我的主要场景(FXMLDocument.fxml控制器):

public class FXMLDocumentController implements Initializable {
    @FXML
    private Button save;
    @FXML 
    private Button edit;
    @FXML
    private TextField textField1;
    ...
    private Stage primaryStage;
    @Override
    @FXML
    public void initialize(URL url, ResourceBundle rb) {
        ...
        //to do?
    }
    ...
    //edit button action
    @FXML
    private void onConfigParametersEdit(ActionEvent event) throws IOException, SchedulerException {

        log.info("You clicked me!");
        FXMLLoader fl = new FXMLLoader();
        fl.setLocation(getClass().getResource("LoginDialog.fxml"));
        fl.load();
        Parent root = fl.getRoot();

        Stage modal_dialog = new Stage(StageStyle.DECORATED);
        modal_dialog.initModality(Modality.WINDOW_MODAL);
        modal_dialog.initOwner(primaryStage);
        Scene scene = new Scene(root);

        LoginDialogController t1 = (LoginDialogController)fl.getController();
        t1.setStage(modal_dialog);
        modal_dialog.setScene(scene);
        modal_dialog.show();
    }

    public void onConfigParametersSave(ActionEvent event) throws IOException {
        String textField1Str = textField1.getText();
        //some code to save it to the database
    }
    ...
}

最后这里是LoginDialog.fxml控制器:

public class LoginDialogController implements Initializable {

    @FXML
    private Button login;
    @FXML 
    private Button cancel;

    @FXML
    private TextField username;
    @FXML
    private TextField password;

    private Stage parentStage;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //to do
    }

    void setStage(Stage temp) {
         parentStage = temp;
    }

    @FXML
    private void onCancelActionEvent(ActionEvent event) {
        log.info("You clicked cancel!");
        parentStage.close();
    }

    @FXML
    private void onLoginActionEvent(ActionEvent event) {
        String usernameStr = username.getText();
        String passwordStr = password.getText();
        log.info("You have entered username: " + usernameStr + " and password: " + passwordStr);
        if(usernameStr.equals("admin") && passwordStr.equals("admin")) {
            log.info("login correct");
            parentStage.close();
            // how am i going to unlock Save button & textField1 into my "parent" scene?
        } else {
            log.info("login error");
        }
    }

问题是我如何在成功登录后返回FXMLDocument.fxml并解锁保存按钮和textField1?

2 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点。这个建议您在LoginDialogController中创建一个额外的字段,用于保存用户凭据是否正确的状态,并将其绑定到FXMLDocumentController中已定义的控件。

public class LoginDialogController implements Initializable {

   ...
   private BooleanProperty loginSuccessful = new SimpleBooleanProperty();

   public loginSuccessfulProperty() {
        return loginSuccessful;
   }

   ...

}

然后当用户成功登录时更新此字段。

@FXML
private void onLoginActionEvent(ActionEvent event) {
    ...
    if(usernameStr.equals("admin") && passwordStr.equals("admin")) {
        log.info("login correct");
        parentStage.close();
        loginSuccessful.set(true);
    } else {
        log.info("login error");
    }
}

从FXMLDocumentController中,让控件editableProperty()绑定到这个新定义的角色。

public class FXMLDocumentController implements Initializable {

    private void onConfigParametersEdit(ActionEvent event) 
         LoginDialogController t1 = (LoginDialogController)fl.getController();
        t1.setStage(modal_dialog);
        textField1.editableProperty().bind(t1.loginSuccessfulProperty());
        save.editableProperty().bind(t1.loginSuccessfulProperty());
    }
}

答案 1 :(得分:0)

好吧我不知道它是否是最佳解决方案但是我设法在这两个场景之间绑定textField1 textField和Save按钮并以这种方式实现所需的功能:

在FXMLDocument.fxml控制器中,我添加了:

@FXML
private void onConfigParametersEdit(ActionEvent event) throws IOException, SchedulerException {

    scheduler.shutdown();

    log.info("You clicked me!");
    FXMLLoader fl = new FXMLLoader();
    fl.setLocation(getClass().getResource("LoginDialog.fxml"));
    fl.load();
    Parent root = fl.getRoot();

    Stage modal_dialog = new Stage(StageStyle.DECORATED);
    modal_dialog.initModality(Modality.WINDOW_MODAL);
    modal_dialog.initOwner(primaryStage);
    Scene scene = new Scene(root);

    LoginDialogController t1 = (LoginDialogController)fl.getController();

    //added these two lines to bind 2nd controller property values
    t1.save2nd = save;
    t1.textField12nd = textField1;

    t1.setStage(modal_dialog);
    modal_dialog.setScene(scene);
    modal_dialog.show();

}

在第二个控制器中,我刚刚在成功登录时添加了两个属性:textField1和save2nd以及setDisable(false):

public class LoginDialogController implements Initializable {
    ...
    //just adding the additional fields - other remains the same
    public Button save2nd;
    public TextField workstationIp2nd;
    ...
    @FXML
    private void onLoginActionEvent(ActionEvent event) {

        String usernameStr = username.getText();
        String passwordStr = password.getText();
        log.info("You have entered username: " + usernameStr + " and password: " + passwordStr);
        if(usernameStr.equals("admin") && passwordStr.equals("admin")) {
            log.info("login correct");

            save2nd.setDisable(false);
            workstationIp2nd.setDisable(false);

            parentStage.close();

        } else {
            log.info("login error");
        }
    }