I am currently new to JavaFx and encountering these errors when trying to pass text from a text field from one scene to another. The entire process is handled by controllers as below:
below is my login controller accessed from the main page:
public class LoginController {
@FXML
public TextField username;
public void login(ActionEvent event) throws IOException {
String usernameInfo = username.getText();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("home.fxml"));
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(fxmlLoader.load());
HomeController home = fxmlLoader.getController();
home.displayName(usernameInfo);
stage.setScene(scene);
stage.setTitle("Home Page");
stage.show();
}
}
below is my home page controller that is supposed to be accessed after a login:
public class HomeController {
@FXML
public Label usernameLabel;
public void displayName(String username) {
usernameLabel.setText("Hi there," + username);
}
}
these are the two errors i am encountering when running the application the moment i click a login button that invokes the login method above
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.IllegalStateException: Location is not set.
Can you help explain what am I doing wrong?
I looked up similar issues where the location to the home.fxml needs to be an absolute path i.e
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("home.fxml"));
to
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("com/example/secondlessons/home.fxml"));
and i still get the same error
Aucun commentaire:
Enregistrer un commentaire