如果场景更改后标签的Javafx setText不起作用 [英] Javafx setText for label is not working if scene is changed after

查看:330
本文介绍了如果场景更改后标签的Javafx setText不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人会解释.我有一个方法,如果登录成功,它会设置标签文本.

maybe someone will explain. I have a method, that sets label text if login is successful.

  @FXML
    private void loginUser(ActionEvent event) throws IOException {
        String user = username.getText();
        String pass = password.getText();
        if(validateFields(user, pass) && validateLogin(user, pass)) {
            welcome.setText("Welcome, " + globalUser.getUserName()); //works 
            infoLine.setText("Redirecting to main dashboard..."); //works
        }
    }

如果我添加其他代码,该代码在登录后更改了场景,则标签文本不会更改:

And if I add additional code, which changes the scene after login, the label text is not changing:

@FXML
private void loginUser(ActionEvent event) throws IOException {
    String user = username.getText();
    String pass = password.getText();
    if(validateFields(user, pass) && validateLogin(user, pass)) {
        welcome.setText("Welcome, " + CurrentUser.getCurrentUser().getUserName());//not working
        infoLine.setText("Redirecting to main dashboard..."); //not working
        
        //Changing scene after successful login
        Parent home = FXMLLoader.load(getClass().getResource(ScenePath.HOME.getPath()));
        Scene homeScene = new Scene(home);
        Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        appStage.setScene(homeScene);
        appStage.show();
    }
}

如何解决这个问题?

我的控制器类看起来像这样.没什么特别的.经过2次验证后,它会设置标签文本并更改场景.

My controller class looks like this. Nothing special. After 2 validations it set texts of labels and changes scenes.

public class LoginController {

       @FXML
        private TextField username;
    
        @FXML
        private PasswordField password;
    
        @FXML
        private Label infoLine;
    
        @FXML
        private Label welcome;
    
        @FXML
        private Button exitBtn;
    
        UserDao userDao = new UserDao();
    
        @FXML
        private void initialize() {
        close();
    }
    
        @FXML
        private void loginUser(ActionEvent event) throws IOException {
            String user = username.getText();
            String pass = password.getText();
            if(validateFields(user, pass) && validateLogin(user, pass)) {
                welcome.setText("Welcome, " + CurrentUser.getCurrentUser().getUserName());
                infoLine.setText("Redirecting to main dashboard...");
    
                //Changing scene after successful login
                Parent home = FXMLLoader.load(getClass().getResource(ScenePath.HOME.getPath()));
                Scene homeScene = new Scene(home);
                Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                appStage.setScene(homeScene);
                appStage.show();
            }
        }
    
        private boolean validateFields(String userName, String password) {
            if (userName.isEmpty() || password.isEmpty()) {
                infoLine.setText("Username and password can't be empty!");
                return false;
            }
            return true;
        }
    
        private synchronized boolean validateLogin(String userName, String password) {
            User user = userDao.getConnectedUser(userName, password);
            if (user == null) {
                infoLine.setText("User not found!");
                return false;
            }
            CurrentUser.setCurrentUser(user);
            return true;
        }
    
        private void close() {
            exitBtn.setOnAction(SceneController::close);
        }
    }

推荐答案

基本上,文本正在更改.问题是,一旦更改了文字,便会加载下一个视图.这真的发生得很快.解决的办法是放慢速度.主要是,在加载新视图之前,让用户有时间查看文本更改.可以使用PauseTransition.

Basically, the text is changing. The problem is as soon as the text is changed, you load the next view. This is happening really fast. The solution is to slow things down. Mainly, give the user time to see the text change before loading the new view. This can be done using PauseTransition.

更改文本后,请尝试以下代码.更改文字后,此代码会在加载新视图之前延迟两秒钟.

After the text change, try the following code. After the text changes, this code gives a two-second delay before loading the new view.

PauseTransition pause = new PauseTransition(Duration.seconds(2));
pause.setOnFinished(
    e -> {
        Parent home = FXMLLoader.load(getClass().getResource(ScenePath.HOME.getPath()));
        Scene homeScene = new Scene(home);
        Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        appStage.setScene(homeScene);
        appStage.show();
    }
);
pause.play();

这篇关于如果场景更改后标签的Javafx setText不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆