I'm new to JavaFX and while doing my project I'm trying to switch screens. I'm thinking of setting of corresponding AnchorPane visibility true or false and I'm having problem when accessing to AnchorPane from another Controller. I tried to make AnchorPane static but it gives NullPointerException.
This is my code.
Controller class
public class Controller {
@FXML
AnchorPane signInPane;
@FXML
private TextField usernameForSignIn;
@FXML
private PasswordField password;
@FXML
private Button signIn;
@FXML
private Button registration;
@FXML
void initialize() {
registration.setOnAction(event -> {
signInPane.setVisible(false);
SignUpController.registerPane.setVisible(true);
});
signIn.setOnAction(event -> {
String usernameText = usernameForSignIn.getText().trim();
String passwordText = password.getText().trim();
if(!usernameText.equals("") && !passwordText.equals("")) {
loginUser(usernameText, passwordText);
} else {
System.out.println("Empty login and/or password");
}
});
}
private void loginUser(String usernameText, String passwordText) {
}
}
SignUpController Class
public class SignUpController {
@FXML
static AnchorPane registerPane;
@FXML
private TextField email;
@FXML
private PasswordField pass;
@FXML
private Button signUp;
@FXML
private TextField fname;
@FXML
private TextField lname;
@FXML
private TextField username;
@FXML
private RadioButton radioMale;
@FXML
void initialize() {
signUp.setOnAction(event -> {
signUpNewUser();
});
}
private void signUpNewUser() {
DatabaseHandler databaseHandler = new DatabaseHandler();
String firstName = fname.getText();
String lastName = lname.getText();
String usname = username.getText();
String password = pass.getText();
String e_mail = email.getText();
String gender = "";
if(radioMale.isSelected()) {
gender = "Male";
} else {
gender = "Female";
}
User user = new User(firstName,lastName,usname,password,e_mail,gender);
databaseHandler.signUpUser(user);
}
}
to this without closing the window
I tried to solve the problem like this, but it closes the window and opens a new one.
registration.setOnAction(event -> {
registration.getScene().getWindow().hide();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/sample/view/signUp.fxml"));
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Parent root = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
// signInPane.setVisible(false);
});