【问题标题】:Disabling a button from another controller in JavaFXML在 JavaFXML 中禁用另一个控制器的按钮
【发布时间】:2018-08-30 18:10:58
【问题描述】:

我正在开发一个 netbeansIDE javaFXML 项目,当我使用用户名和密码登录到第二个屏幕时,我希望禁用第二个屏幕按钮。我试图通过使用 Getter 来禁用另一个控制器中的按钮,但是当我登录时没有任何反应。 我真的希望能有所帮助,我尝试搜索帮助并找到了一些帮助,并尝试但以这个结束,现在我被卡住了。

这是我的代码:

{@FXML
private void handleLogin(ActionEvent event) throws IOException {

    String username = txtUsername.getText();
    String password = txtPassword.getText();

    Button pressed_button = (Button) event.getSource();

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("Screen3_Scada.fxml"));
    loader.load();
     if(pressed_button==btnscadalogin){
        if (loginLoad.validateSCADA(username, password)) {

            try {
                AnchorPane pane = FXMLLoader.load(getClass().getResource("Screen3_Scada.fxml"));
                 RootPane.getChildren().setAll(pane);

                 Screen3_Scada_Controller getbtnPLC = loader.getController();
                 getbtnPLC.getBtngotoPLC().setDisable(true);

             }catch (IOException ex) {
                System.err.println("Error in method GotoScreen2 class FXML_Screen1_loginController");
             }

        }else{
            System.err.println("Error, wrong username or password");
        }


    }
}}

【问题讨论】:

    标签: java javafx scenebuilder netbeans-platform


    【解决方案1】:

    您正在加载场景两次:

    loader.load();
    ...
    AnchorPane pane = FXMLLoader.load(getClass().getResource("Screen3_Scada.fxml"));
    

    getbtnPLC 不是在您加载添加到现有场景的场景时创建的控制器实例。

    您应该使用 loader.load(); 调用结果的加载结果:

    if (pressed_button == btnscadalogin) {
        if (loginLoad.validateSCADA(username, password)) {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("Screen3_Scada.fxml"));
    
            try {
                AnchorPane pane = loader.load();
                RootPane.getChildren().setAll(pane);
    
                Screen3_Scada_Controller getbtnPLC = loader.getController();
                getbtnPLC.getBtngotoPLC().setDisable(true);
    
            } catch (IOException ex) {
                System.err.println("Error in method GotoScreen2 class FXML_Screen1_loginController");
            }
       }
    

    我确实建议使用不同的方法来处理来自不同来源的ActionEvents。这通常比检查多个节点中的哪一个是事件源更好的选择。

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多