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);
});
Create a single window and reuse it, instead of creating a new one each time. Then you can either just check if it is showing, or you can disable whichever control shows it when it is showing.
Something like this will ensure that your FXML is loaded before the initalize(). You do not have to hide your AnchorPane, as the instances are already loaded. Hiding items created from FXML usually causes errors.
public class Controller {
@FXML
Button button_CustomerInfo;
@FXML
Button button_A1Child;
@FXML
BorderPane bPane;
@FXML
ToggleGroup tog;
Parent r1;
Parent r2;
// CREATE SOMETHING LIKE THIS
// THIS SAMPLE CODE USES THE SAME BORDERPANE
// AND THE CENTER OF THE BORDERPANE
// IS CHANGED WHEN A BUTTON IS PRESSED
{
try {
r1 = FXMLLoader.load(getClass().getResource("SecondSreen.fxml"));
r2 = FXMLLoader.load(getClass().getResource("FirstScreen.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialize() {
button_CustomerInfo.setOnAction(e-> {
bPane.setCenter(r2);
});
button_A1Child.setOnAction(e-> {
bPane.setCenter(r1);
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.