1
Friends, this is my main start method of a Javafx project. The idea was for me to store a cache of all the screens so that I could pass objects between the screens through the Observer Pattern. The pass-through method is "Changescreen".
public void start(Stage primaryStage) {
try {
stage = primaryStage;
primaryStage.setTitle("MyApp");
AnchorPane fxmlLogin = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/ViewLogin.fxml"));
LoginScene = new Scene(fxmlLogin,1077,720);
AnchorPane fxmlAlterDados = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/AlterarDadosView.fxml"));
AlterDadosScene = new Scene(fxmlAlterDados,1078,720);
AnchorPane fxmlMain = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/ViewMainUsuario.fxml"));
MainScene = new Scene(fxmlMain, 1077, 720);
AnchorPane fxmlDetalhes = (AnchorPane)FXMLLoader.load(getClass().getResource("/com/quixada/ufc/fbd/view/DetalhesPontoTurisiticoView.fxml"));
DetalhesScene = new Scene(fxmlDetalhes, 1077, 720);
primaryStage.setScene(LoginScene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void changeScreen(String scr, Object userData) {
switch (scr) {
case "login":
stage.setScene(LoginScene);
notifyAllListeners("login", userData);
break;
case "cadastro":
stage.setScene(CadastroScene);
notifyAllListeners("cadastro", userData);
break;
case "main":
stage.setScene(MainScene);
notifyAllListeners("main", userData);
break;
case "alterDados":
stage.setScene(AlterDadosScene);
notifyAllListeners("alterDados", userData);
}
}
public static void changeScreen(String scr) {
changeScreen(scr, null);
}
public static void main(String[] args) {
launch(args);
}
public static void addOnChangeScreenListener(OnChangeScreen newListener) {
listeners.add(newListener);
}
private static void notifyAllListeners(String newScreen, Object userData) {
for (OnChangeScreen l : listeners)
l.onScreenChanged(newScreen, userData);
}
}
When logging in, the database will return a corresponding user instance so that it can be passed to the user’s main screen. From this object passed to the main screen, would be loaded, from it the Labels that would have the name and age of the same. However, the problem is that when performing this action, a Nullpointerexception problem occurs.
public void initialize(URL location, ResourceBundle resources) {
MainJavaFX.addOnChangeScreenListener(new OnChangeScreen() {
@Override
public void onScreenChanged(String newScreen, Object userData) {
// TODO Auto-generated method stub
userNow = (Turista) userData;
}
});
idNameLabel.setText(userNow.getTnome());
String idade = Integer.toString(userNow.getTidade());
idIdadeLabel.setText(idade);
Could someone give me some idea or solution to help me solve this problem?
You could put the User object as Static variable in the main class of your application. It is difficult to pass parameters between FXML screens.
– Gustavo Fragoso
I think define variables Static for communication between screens, not be a good practice. A good practice is the use of class builders to send any information, even to execute methods. I had already answered an equal question on this forum, give a look. https://answall.com/a/307343/65598
– Julio Cesar