In my program the user the user enters a password on an external window, and then that password is passed through a function. The problem is that the function is executed before the user can enter a password. Here is my password window. Passw has already been declared as a static string for the class
public static String enterPassword()
{
Stage primaryStage = new Stage();
VBox vb = new VBox();
vb.setPadding(new Insets(10, 0, 0, 10));
vb.setSpacing(10);
HBox hb = new HBox();
hb.setSpacing(10);
hb.setAlignment(Pos.CENTER_LEFT);
Label label = new Label("Password");
final PasswordField pb = new PasswordField();
final TextField textField = new TextField();
textField.setManaged(false);
textField.setVisible(false);
CheckBox checkBox = new CheckBox("Show/Hide password");
textField.managedProperty().bind(checkBox.selectedProperty());
textField.visibleProperty().bind(checkBox.selectedProperty());
pb.managedProperty().bind(checkBox.selectedProperty().not());
pb.visibleProperty().bind(checkBox.selectedProperty().not());
// Bind the textField and passwordField text values
bidirectionally.
textField.textProperty().bindBidirectional(pb.textProperty());
pb.setOnAction(e ->
{
passw = pb.getText();
pb.clear();
primaryStage.hide();
});
textField.setOnAction(e ->
{
passw = textField.getText();
textField.clear();
primaryStage.hide();
});
hb.getChildren().addAll(label, pb, textField, checkBox);
vb.getChildren().addAll(hb);
Scene scene = new Scene(vb, 450, 90);
primaryStage.setScene(scene);
primaryStage.show();
return passw;
}
And here is the code in main where the function hostRun is executed
password = enterPassword();
while(password == null)
{
}
hostRun(4000000, '0', index, password, 0);
Here I try a while loop to delay the execution of hostRun but even that causes the password window to crash. How do I delay the execution of hostRun until the user enters a password?
If you want to pause your program until the stage closed you can do that with:
primaryStage.showAndWait();
This will pause the method that invoked the stage until it closes. No need for a loop. Then enterPassword() will wait until the stage closes.
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.