I am creating a Library System using JavaFX where you can lend items to the members. I've created a login that takes me to another fxml file called main-view.fxml which has a controller called MainWindowController.
The problem I have is that after successful login, the new Window/Scene displays "Welcome, null" but what I expect the program to do is welcome the logged-in user by his username.
A screenshot to show how the main window looks and the problem I am trying to solve can be found below. I want it to Welcome the user by its name. I have used welcomeLabel.SetText();
to get username by getter getUsername();
from User class.
I believe I get null because the data when we log in is not carried forward to the next scene. I am not sure how to do that. Hope I have explained the problem well. I have searched about this a lot on this platform but could not find an answer that completely solves my worries.
I have pasted the code for MainWindowController, LoginController, User class, and a hardcoded Database. Thank you in advance.
A screenshot of main window](https://i.stack.imgur.com/fMXp7.png)
The Login controller class:
public class LoginController {
@FXML
private Label loginStatus;
@FXML
private TextField usernameField;
@FXML
private TextField passwordField;
@FXML
private Button loginButton;
Database database;
public LoginController() {}
@FXML
public void onLoginButtonClicked(ActionEvent actionEvent) throws IOException {
database = new Database();
for (User user : database.getUsers()) {
if (usernameField.getText().equals(user.getUsername()) &&
(passwordField.getText().equals(user.getPassword()))) {
Stage loginStage = (Stage) loginButton.getScene().getWindow();
loginStage.close();
MainWindowController mainWindowController = new MainWindowController();
mainWindowController.loadScene("main-view.fxml");
} else if (usernameField.getText().isEmpty() && passwordField.getText().isEmpty()) {
loginStatus.setText("Please enter your username and password.");
} else {
loginStatus.setText("Wrong credentials, please try again.");
}
}
}
}
The MainWindow controller class:
public class MainWindowController implements Initializable {
Database database;
User loggedUser;
public MainWindowController() {}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
database = new Database();
loggedUser = new User();
lblWelcome.setText("Welcome, " + loggedUser.getUsername());
}
static public void loadScene(String name) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource(name));
Stage stage = new Stage();
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Lending/Receiving - Library System");
stage.setScene(scene);
stage.showAndWait();
}
}
The User class:
public class User {
private String username;
private String password;
public User(){ }
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) { this.username = username; }
public String getPassword() {
return password;
}
public void setPassword(String password) {this.password = password; }
}
The database class, just in case, it is needed:
public class Database {
private List<User> users = new ArrayList<>();
public List<User> getUsers() { return users; }
public Database() {
//users
users.add(new User("u", "p"));
users.add(new User("admin", "admin"));
}
}