在对话框上执行字段检查时不显示JavaFX警报
我是JavaFX的初学者,正在一个小应用程序上练习。问题是,我试图在对话框字段为空时执行检查,如果为空,则填充警告类型的警报,但未成功。 如果未填写所有字段,则该对话框确实没有输入数据,但警报从未显示 这是主控制器代码:在对话框上执行字段检查时不显示JavaFX警报,javafx,Javafx,我是JavaFX的初学者,正在一个小应用程序上练习。问题是,我试图在对话框字段为空时执行检查,如果为空,则填充警告类型的警报,但未成功。 如果未填写所有字段,则该对话框确实没有输入数据,但警报从未显示 这是主控制器代码: @FXML public void showAddReportDialog () { Dialog<ButtonType> dialog = new Dialog<ButtonType>(); dialog.i
@FXML
public void showAddReportDialog () {
Dialog<ButtonType> dialog = new Dialog<ButtonType>();
dialog.initOwner(mainPanel.getScene().getWindow());
dialog.setTitle("Add new report");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("addReportDialog.fxml"));
try {
dialog.getDialogPane().setContent(fxmlLoader.load());
} catch (IOException e) {
System.out.println("Could not load the dialog");
e.printStackTrace();
return;
}
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
final Button btOK = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
btOK.addEventFilter(ActionEvent.ACTION, event -> {
AddDialogController addDialogController = fxmlLoader.getController();
Report newReport = addDialogController.getNewReport();
if (newReport != null) {
data.addReport(newReport);
data.saveReports();
} else {
event.consume();
}
});
Optional<ButtonType> result = dialog.showAndWait();
}
非常感谢你的帮助 您可以使用执行此操作,但是
对话框将在警报关闭后始终关闭。我建议将登录按钮
绑定到文本字段
回答您的问题:
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Pair;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
Button button = new Button("Show Add Report Dialog");
button.setOnAction((e) -> {
showAddReportDialog();
});
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
public void showAddReportDialog () {
//Code from https://code.makery.ch/blog/javafx-dialogs-official/.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");
// Set the icon (must be included in the project).
//dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
//loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
// loginButton.disableProperty().bind(Bindings.createBooleanBinding(
// () -> username.getText().isEmpty() || password.getText().isEmpty(),
// username.textProperty(), password.textProperty()));
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
if(username.getText().isBlank() || password.getText().isBlank())
{
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Fields incomplete");
alert.setHeaderText(null);
alert.setContentText("Please complete all the form fields");
alert.showAndWait();
}
else
{
return new Pair<String, String>(username.getText(), password.getText());
}
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(usernamePassword -> {
System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
});
}
}
谢谢绑定有帮助,我发现由于对话框字段的验证,我的警报没有显示。当我使用:if((iDField.getText()==null | | iDField.getText().trim().isEmpty())而不是:if(iDField.getText().equals(“”))时,警报工作了!
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Pair;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
Button button = new Button("Show Add Report Dialog");
button.setOnAction((e) -> {
showAddReportDialog();
});
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
public void showAddReportDialog () {
//Code from https://code.makery.ch/blog/javafx-dialogs-official/.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");
// Set the icon (must be included in the project).
//dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
//loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
// loginButton.disableProperty().bind(Bindings.createBooleanBinding(
// () -> username.getText().isEmpty() || password.getText().isEmpty(),
// username.textProperty(), password.textProperty()));
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
if(username.getText().isBlank() || password.getText().isBlank())
{
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Fields incomplete");
alert.setHeaderText(null);
alert.setContentText("Please complete all the form fields");
alert.showAndWait();
}
else
{
return new Pair<String, String>(username.getText(), password.getText());
}
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(usernamePassword -> {
System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
});
}
}
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Pair;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
Button button = new Button("Show Add Report Dialog");
button.setOnAction((e) -> {
showAddReportDialog();
});
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
public void showAddReportDialog () {
//Code from https://code.makery.ch/blog/javafx-dialogs-official/.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");
// Set the icon (must be included in the project).
//dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
loginButton.disableProperty().bind(Bindings.createBooleanBinding(
() -> username.getText().isEmpty() || password.getText().isEmpty(),
username.textProperty(), password.textProperty()));
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(username.getText(), password.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(usernamePassword -> {
System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
});
}
}