I have the following workflow in my application that is causing a problem:
Click Button to Open Dialog > Open Dialog > Click Button From Dialog > Display Confirmation Alert > Upon Confirmation close the first dialog and open a new dialog
The second dialog does not allow input into the TextField. I have included a SSCE that displays the problem. One additional weird thing is that if you try to close the second dialog by clicking the 'X', and then close the Alert then I am able to type into the field.
public class DialogTest extends Application {
#Override
public void start(Stage primaryStage) {
Button button = new Button("Show Dialog");
VBox root = new VBox(10, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 350, 120);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnAction(event -> {
Dialog<Pair<String, String>> dialog = getDialog(scene.getWindow(), "Dialog 1", true);
dialog.showAndWait();
});
}
public static void main(String[] args) {
launch(args);
}
public Dialog<Pair<String, String>> getDialog(Window owner, String title, boolean addButton) {
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle(title);
dialog.initOwner(owner);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
if(addButton) {
Button button = new Button("Show Dialog");
dialog.getDialogPane().setContent(button);
button.setOnAction(event -> {
Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure?", ButtonType.YES, ButtonType.NO);
alert.initOwner(owner);
if(alert.showAndWait().get() == ButtonType.YES) {
dialog.close();
Dialog<Pair<String, String>> dialog2 = getDialog(owner, "Dialog 2", false);
TextField textField = new TextField();
dialog2.getDialogPane().setContent(textField);
dialog2.getDialogPane().getScene().getWindow().setOnCloseRequest(closeEvent -> {
closeEvent.consume();
if(textField.getText().trim().isEmpty()) {
Alert alert2 = new Alert(AlertType.ERROR, "Please enter a value", ButtonType.OK);
alert2.initOwner(dialog2.getDialogPane().getScene().getWindow());
alert2.showAndWait();
}
});
dialog2.showAndWait();
}
});
}
return dialog;
}
}
Problem
As explained, you have a modality problem.
Solution
The following code will demonstrate a solution where the user is asked if he really wants to print and after printing, if the ending number is correct.
(Note, that I use a class IntField from here)
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
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.ProgressIndicator;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.Window;
public class DialogTest extends Application {
Region veil;
ProgressIndicator indicator;
IntField startingNumber = new IntField(0, 999999, 0);
IntField endingNumber = new IntField(startingNumber.getValue(), 999999, startingNumber.getValue() + 1);
ButtonType printButtonType = new ButtonType("Print", ButtonData.OK_DONE);
Stage stage;
#Override
public void start(Stage primaryStage) {
stage = primaryStage;
Button button = new Button("Print Checks");
VBox box = new VBox(10, button);
box.setAlignment(Pos.CENTER);
veil = new Region();
veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.3);");
veil.setVisible(false);
indicator = new ProgressIndicator();
indicator.setMaxHeight(60);
indicator.setMinWidth(60);
indicator.setVisible(false);
StackPane root = new StackPane(box, veil, indicator);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnAction((event) -> {
Dialog<ButtonType> dialog
= getCheckPrintDialog(primaryStage, "Enter starting check number");
dialog.showAndWait()
.filter(result -> result == printButtonType)
.ifPresent(result -> {
// this is for this example only, normaly you already have this value
endingNumber.setValue(startingNumber.getValue() + 1);
printChecks(startingNumber.getValue(), endingNumber.getValue());
});
});
}
public static void main(String[] args) {
launch(args);
}
public <R extends ButtonType> Dialog getCheckPrintDialog(Window owner, String title) {
Dialog<R> dialog = new Dialog<>();
dialog.initOwner(owner);
dialog.setTitle(title);
dialog.getDialogPane().getButtonTypes().addAll(printButtonType, ButtonType.CANCEL);
Button btOk = (Button) dialog.getDialogPane().lookupButton(printButtonType);
btOk.addEventFilter(ActionEvent.ACTION, event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Print Checks? Are you sure?", ButtonType.YES, ButtonType.NO);
alert.showAndWait()
.filter(result -> result == ButtonType.NO)
.ifPresent(result -> event.consume());
});
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
Text from = new Text("Starting Number:");
grid.add(from, 0, 0);
grid.add(startingNumber, 1, 0);
dialog.getDialogPane().setContent(grid);
return dialog;
}
private void printChecks(int from, int to) {
Task<Void> task = new Task<Void>() {
#Override
protected Void call() throws Exception {
Thread.sleep(5000);
return null;
}
};
task.setOnSucceeded((event) -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Has the last check, the number: " + endingNumber.getValue() + "?", ButtonType.YES, ButtonType.NO);
alert.initOwner(stage);
Button btnNo = (Button) alert.getDialogPane().lookupButton(ButtonType.NO);
btnNo.addEventFilter(ActionEvent.ACTION, e -> {
Dialog<ButtonType> newEndNum = new Dialog<>();
newEndNum.setTitle("Enter the ending check number");
newEndNum.initOwner(stage);
newEndNum.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
Text toUser = new Text("Ending Number:");
grid.add(toUser, 0, 0);
grid.add(endingNumber, 1, 0);
newEndNum.getDialogPane().setContent(grid);
newEndNum.showAndWait().filter(result -> result == ButtonType.CANCEL)
.ifPresent(result -> e.consume());
});
alert.showAndWait();
});
veil.visibleProperty().bind(task.runningProperty());
indicator.visibleProperty().bind(task.runningProperty());
new Thread(task).start();
}
}
Working Application
The main Window:
The Print Dialog:
After a click on Print (Alerts are localized, for me in german):
After a click on Yes the Print Dialog closes and a progress will be visible (for 5 sec. in this example)
After the Printing finishes a Dialog comes up which is asking for the correct ending number
If you click Yes all is done, if you click No another dialog opens to enter the correct ending value
I have found a point where the problem is. But because I am just beginning with JavaFx I cannot provide the "why".
It seems to me that the problem is in the dialog.close(), just after the if (alert.showAndWait().get() == ButtonType.YES).
Look like it looses some reference to the dialog when you close it or something like that (I let the experts clear this out).
As a workaround, and it works for me, is move the dialog.close() to after dialog2.showAndWait();
public Dialog<Pair<String, String>> getDialog(Window owner, String title, boolean addButton) {
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle(title);
dialog.initOwner(owner);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
if (addButton) {
Button button = new Button("Show Dialog");
dialog.getDialogPane().setContent(button);
button.setOnAction(event -> {
Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure?", ButtonType.YES, ButtonType.NO);
alert.initOwner(owner);
if (alert.showAndWait().get() == ButtonType.YES) {
// dialog.close(); // supressed this and placed at the bottom
Dialog<Pair<String, String>> dialog2 = getDialog(owner, "Dialog 2", false);
TextField textField = new TextField();
dialog2.getDialogPane().setContent(textField);
dialog2.getDialogPane().getScene().getWindow().setOnCloseRequest(closeEvent -> {
closeEvent.consume();
if (textField.getText().trim().isEmpty()) {
Alert alert2 = new Alert(AlertType.ERROR, "Please enter a value", ButtonType.OK);
alert2.initOwner(dialog2.getDialogPane().getScene().getWindow());
alert2.showAndWait();
}
});
dialog2.showAndWait();
dialog.close(); // new location
}
});
}
return dialog;
}
The reasons for this to happen I cannot explain, but this could be a workaround.
I hope this helps you.
In your "start" method where you create Dialog1 you should call dialog.show() instead of dialog.showAndWait().
i.e.
button.setOnAction(event -> {
Dialog<Pair<String, String>> dialog = getDialog(scene.getWindow(), "Dialog 1", true);
// dialog.showAndWait();
dialog.show();
});
Related
I'm currently working on a password manager. Before making any changes to a certain service, the program will ask the user for a password for authorization and then proceed to show the appropriate dialog, if the password is correct.
The issue that I'm having is that if I go through the cycle of putting in my password to make the change, click "ok", and then proceeding to make changes on the shown dialog, on the next turn if instead of putting the password when prompted I close the prompt, then the program retrieves the password from the previous iteration although it has been explicitly cleared. Resulting in the concurrent dialog showing, which is only supposed to show if you put in the correct password.
private void handleEditButton(MouseEvent event) {
Optional<String> rslt = passwordConfirmDialog.showAndWait();
if (rslt.get().equals(""))
return; //Do not proceed
String userInput = rslt.get().trim();
// Complex expression, but use of && statement is necessary to avoid an
// unecessary call to db and have return statement on this IF
if (!(!userInput.isBlank() && isCorrectPassword(userInput))) {
// show dialog
AlertConfigs.invalidPasswordTransactionFailed.showAndWait();
return;
}
System.out.println("Edit Handler: Correct password. -> " + userInput);
//Proceed to show next dialog...
private void initializePasswordConfirmDialog() {
passwordConfirmDialog.setTitle("User Account Control");
passwordConfirmDialog.setHeaderText("Please enter your password to continue.");
// Set the button types.
ButtonType ok = new ButtonType("Ok", ButtonData.OK_DONE);
passwordConfirmDialog.getDialogPane().getButtonTypes().addAll(ok, ButtonType.CANCEL);
final PasswordField psField = new PasswordField();
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
grid.add(new Label("Please Enter your password"), 0, 0);
grid.add(psField, 1, 0);
passwordConfirmDialog.getDialogPane().setContent(grid);
passwordConfirmDialog.setResultConverter(buttonType -> {
String rslt = "";
if (buttonType == ok) {
rslt = psField.getText();
}
psField.clear();
return rslt;
});
}
I've posted a video on YouTube to help visualize the problem. https://youtu.be/sgayh7Q7Ne8
The PasswordField in initializePasswordConfirmDialog() is cleared because whenever I run the the prompt the second time, the PasswordField is blank (visually). Nevertheless, for some reason it still grabs the result from the previous iteration.
The initializePasswordConfirmDialog() is called once inside the constructor and is responsible for set the passwordConfirmDialog variable with the adequate properties.
Some additional code:
HomeController.java
#FXML
private GridPane servicesGrid;
private Dialog<String> passwordConfirmDialog;
private Dialog<Service> editServiceDialog;
private final int NUM_COLUMNS = 7;
public HomeController() {
passwordConfirmDialog = new Dialog<>();
initializePasswordConfirmDialog();
editServiceDialog = new Dialog<>();
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
loadServicesGridpane();
}
private void loadServicesGridpane() {
ArrayList<Service> currS = acct.getServices();
// int currentRow = 1;
for (Service s : currS)
addRowToServiceGrid(s);
}
private void addRowToServiceGrid(Service s) {
int rowIdx = servicesGrid.getChildren().size() / 4;
Button editButton = new Button("Edit");
editButton.setOnMouseClicked(event -> {
handleEditButton(event);
});
Button deleteButton = new Button("Delete");
deleteButton.setOnMouseClicked(event -> {
handleDeleteButton(event);
});
deleteButton.setId(s.getServiceName());
Label currServiceName = new Label(s.getServiceName());
currServiceName.setId(s.getServiceName());
Label currUsername = new Label(s.getServiceUsername());
Label currPassword = new Label(s.getServicePassword());
Label dateCreated = new Label(s.getDateCreated());
Label lastPssdChange = new Label(s.getLastPasswordChange());
servicesGrid.addRow(rowIdx, currServiceName, currUsername, currPassword, dateCreated, lastPssdChange,
deleteButton, editButton);
}
To study the problem in isolation, I refactored this example to permit reusing the dialog. As shown below, reusing the dialog requires clearing the password field. Replace the parameter dialog with an invocation of createDialog() to see that creating the dialog each time does not require clearing the password field. Comparing the profile of each approach may help you decide which approach is acceptable; in my experiments, reuse added negligible memory overhead (~250 KB), and it protracted garbage collection slightly(~50 ms).
#!/bin/sh
java … DialogTest -reuse &
pid1=$!
java … DialogTest -no-reuse &
pid2=$!
echo $pid1 $pid2
jconsole $pid1 $pid2
Unfortunately, creating the dialog each time may only appear to solve the problem; it may have exposed a latent synchronization problem. In particular, verify that your result converter's callback executes on the JavaFX Application Thread. To illustrate, I've added a call to Platform.isFxApplicationThread() in resultsNotPresent() below.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* #see https://stackoverflow.com/q/73328282/230513
* #see https://stackoverflow.com/a/44172143/230513
*/
public class DialogTest extends Application {
private static boolean REUSE_DIALOG = true;
private record Results(String text, String pass) {
private static Results of(String text, String pass) {
return new Results(text, pass);
}
}
#Override
public void start(Stage stage) {
var label = new Label("Reuse: " + REUSE_DIALOG);
var button = new Button("Button");
if (REUSE_DIALOG) {
var dialog = createDialog();
button.setOnAction(e -> showDialog(dialog));
} else {
button.setOnAction(e -> showDialog(createDialog()));
}
stage.setScene(new Scene(new HBox(8, label, button)));
stage.show();
}
private Dialog<Results> createDialog() {
var dialog = new Dialog<Results>();
dialog.setTitle("Dialog Test");
dialog.setHeaderText("Please authenticate…");
var dialogPane = dialog.getDialogPane();
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
var text = new TextField("Name");
var pass = new PasswordField();
dialogPane.setContent(new VBox(8, text, pass));
dialog.showingProperty().addListener((o, wasShowing, isShowing) -> {
if (isShowing) {
Platform.runLater(pass::requestFocus);
}
});
dialog.setResultConverter((ButtonType bt) -> {
if (ButtonType.OK == bt) {
var results = Results.of(text.getText(), pass.getText());
pass.clear();
return results;
}
pass.clear();
return null;
});
return dialog;
}
private void showDialog(Dialog<Results> dialog) {
var optionalResult = dialog.showAndWait();
optionalResult.ifPresentOrElse(
(var results) -> System.out.println(results),
(this::resultsNotPresent));
}
private void resultsNotPresent() {
System.out.println("Canceled on FX application thread: "
+ Platform.isFxApplicationThread());
}
public static void main(String[] args) {
if (args.length > 0) {
REUSE_DIALOG = args[0].startsWith("-r");
}
launch(args);
}
}
Here is a small sample from my custom dialog, which is meant to display the progress of a running javafx.concurrent.Task.
DialogPane pane = this.getDialogPane()
pane.getButtonTypes().addAll(ButtonType.CANCEL);
pane.headerTextProperty().bind(task.titleProperty());
pane.contentTextProperty().bind(task.messageProperty());
For some reason, the buttons in the button bar disappeared, but only after some text updated. After further investigation, I found that binding the header text of a dialog seems to remove all the buttons in the button bar. Why would this happen, and what would I do to stop the buttons from being hidden?
EDIT: Here's an MCVE demonstrating the problem.
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MCVE extends Application {
public static class CustomDialog extends Dialog<ButtonType> {
public CustomDialog(Task<?> task) {
this.initModality(Modality.APPLICATION_MODAL);
DialogPane pane = this.getDialogPane();
{
pane.getButtonTypes().addAll(ButtonType.CANCEL);
pane.headerTextProperty().bind(task.titleProperty());
}
setOnCloseRequest(event -> {
if (task.isRunning()) event.consume();
});
}
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
StackPane root = new StackPane();
Button starter = new Button("Showcase");
starter.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
root.getChildren().add(starter);
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.show();
starter.setOnAction(event -> {
Task<Void> task = new Task<>() {
#Override
protected Void call() throws Exception {
updateTitle("Before loop");
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
if (isCancelled()) return null;
}
for (int i = 1; i <= 20; i++) {
updateTitle("loop " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
if (isCancelled()) return null;
}
}
return null;
}
};
Thread worker = new Thread(task);
worker.start();
new CustomDialog(task).showAndWait();
});
}
}
It appears that the issue needs fixing on JavaFX's end. In fact, the button bar is simply pushed out of view. There is a relatively decent workaround though. Dialog internally uses a styled GridPane to display header text and graphics, so I simply replicated that using an external GridPane and instead bound to the textProperty() of a Label.
public static class CustomDialog extends Dialog<ButtonType> {
public CustomDialog(Task<?> task) {
DialogPane pane = this.getDialogPane(); {
pane.getButtonTypes().addAll(ButtonType.CANCEL);
//construct custom header
GridPane headerRoot = new GridPane(); {
Label headerText = new Label();
//headerText is the label containing the header text
headerText.textProperty().bind(task.titleProperty());
headerRoot.add(headerText, 0, 0);
}
headerRoot.getStyleClass().addAll("header-panel");
pane.setHeader(headerRoot);
pane.setContentText("Placeholder content");
pane.getScene().getWindow().setOnCloseRequest(event -> {
if (!task.isDone()) {
event.consume();
}
});
}
}
It looks exactly the same as a default dialog, without the issue mentioned in the question above.
I have a form Dialog and I want to add a confirmation alert before the dialog closes.
The dialog is not to close until confirmed Ok.
This I can prevent by consuming the event.
It should only confirm on clicking the Dialog Ok, not on Cancel.
Problem is, I cannot see which button was clicked on the event. So the Alert confirmation is shown for both OK and CANCEL buttons on the dialog.
How can I prevent the onClosingReqeust for cancel button?
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
public class DialogTestApplication extends Application {
public static void main(String[] args) {
launch(args);
}
class MyDialog extends Dialog<ButtonType> {
public MyDialog(Window window) {
setTitle("MyDialog");
initModality(Modality.WINDOW_MODAL);
initOwner(window);
setResizable(true);
GridPane contentPane = new GridPane();
contentPane.add(new TextField(), 0, 0);
getDialogPane().setContent(contentPane);
getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
setOnCloseRequest(event -> {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.CANCEL) {
event.consume();
}
});
});
}
}
#Override
public void start(Stage primaryStage) throws Exception {
final StackPane root = new StackPane();
final Label rootLabel = new Label("DialogTestApplication");
root.getChildren().add(rootLabel);
final Scene scene = new Scene(root, 400, 150);
primaryStage.setScene(scene);
primaryStage.setTitle("DialogTestApplication");
primaryStage.show();
Platform.setImplicitExit(true);
MyDialog dialog = new MyDialog(primaryStage);
dialog.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
System.out.println("OK");
}
Platform.exit();
});
}
}
After reading the Dialog Javadoc I found a solution that works.
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html
Button button = (Button) getDialogPane().lookupButton(ButtonType.OK);
button.addEventFilter(ActionEvent.ACTION, event -> {
final Alert alert = new Alert(AlertType.CONFIRMATION);
alert.initOwner(window);
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.CANCEL) {
event.consume();
}
});
});
final Button btnCancel = (Button) dialog.getDialogPane().lookupButton( ButtonType.CANCEL);
btnCancel.addEventFilter(ActionEvent.ACTION, event -> {
// handle cancel button code here
event.consume();
});
public boolean displayDialog() {
Optional<ButtonType> result = dialog.showAndWait();
if(result.get().getText().equals("Ok")) {
//handle OK action. The dialog panel will close automatically.
}
return true;
}
Handle the cancel button event when instantiating the alert. The event.consume() method will prevent the alert dialog from closing when pressing CANCEL. You can add a method to display your alert, within that method handle the OK action.
You can handle action event of the OK button instead of handling close request of the dialog. Something like this
// I tested with onMouseClicked but the event doesn't get fired
// onAction on the other hand works normally but we need to cast the node to a button
Button button = (Button) getDialogPane().lookupButton(ButtonType.OK);
button.setOnAction(e -> {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.CANCEL) {
event.consume();
}
});
});
I tried using this method to print show a popup with label "THAT'S IT" and I don't want to use the Popup class
public void showStage(Stage Owner){
HBox hBox = new HBox();
hBox.getChildren().add(new Label("THAT'S IT"));
Scene sc = new Scene(hBox);
Stage popup = new Stage();
popup.setScene(sc);
popup.setWidth(400);
popup.setHeight(100);
popup.initOwner(owner);
popup.initModality(Modality.WINDOW_MODAL);
popup.show();
}
and then I call the showStage() method from the start method
public void start(Stage primaryStage) {
Label lb = new Label();
btn.setText("Say 'Hello World'");
btn.setOnAction(e->{
lb.setText("hello everyone");showStage(primaryStage);
});
But the output of the code :
Why don't you use a dialog?
You can use it in your main controller class without creating an other stage for instance like this:
Dialog dialogQtPrescription = new Dialog();
dialogQtPrescription.setTitle("yourTitle");
dialogQtPrescription.setHeaderText("yourHeadertext");
dialogQtPrescription.initModality(Modality.WINDOW_MODAL);
dialogQtPrescription.initOwner(mainStage);
dialogQtPrescription.initStyle(StageStyle.UTILITY);
GridPane gridDialogPrescription = new GridPane();
gridDialogPrescription.setHgap(10);
gridDialogPrescription.setVgap(10);
gridDialogPrescription.add(new Label(bundle.getString("quantityPrescription.title")), 0, 0);
TextField txtQtPrescr = new TextField();
ButtonType buttonTypeNo = new ButtonType("no");
ButtonType buttonTypeYes = new ButtonType("yes");
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
dialogQtPrescription.getDialogPane().getButtonTypes().addAll(buttonTypeNo,buttonTypeYes, buttonTypeCancel);
txtQtPrescr.setPrefWidth(100);
gridDialogPrescription.add(txtQtPrescr, 1, 0);
dialogQtPrescription.getDialogPane().setContent(gridDialogPrescription);
Optional<ButtonType> result = dialogQtPrescription.showAndWait();
this is just a stack of code from a project but i hope it make you to understand my idea.
here's better explained: http://code.makery.ch/blog/javafx-dialogs-official/
You can use this in order to make a Popup on any Screen in JavaFX
public void popup() {
final Stage dialog = new Stage();
dialog.setTitle("Confirmation");
Button yes = new Button("Yes");
Button no = new Button("No");
Label displayLabel = new Label("What do you want to do ?");
displayLabel.setFont(Font.font(null, FontWeight.BOLD, 14));
dialog.initModality(Modality.NONE);
dialog.initOwner((Stage) tableview.getScene().getWindow());
HBox dialogHbox = new HBox(20);
dialogHbox.setAlignment(Pos.CENTER);
VBox dialogVbox1 = new VBox(20);
dialogVbox1.setAlignment(Pos.CENTER_LEFT);
VBox dialogVbox2 = new VBox(20);
dialogVbox2.setAlignment(Pos.CENTER_RIGHT);
dialogHbox.getChildren().add(displayLabel);
dialogVbox1.getChildren().add(yes);
dialogVbox2.getChildren().add(no);
yes.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
// inside here you can use the minimize or close the previous stage//
dialog.close();
}
});
no.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
dialog.close();
}
});
dialogHbox.getChildren().addAll(dialogVbox1, dialogVbox2);
Scene dialogScene = new Scene(dialogHbox, 500, 40);
dialogScene.getStylesheets().add("//style sheet of your choice");
dialog.setScene(dialogScene);
dialog.show();
}
I hope I'm not duplicating a question, but I couldn't find one specifically for my issue.
I'm developing a small math flash card application, using JavaFX to create the GUI. The program should runs as follow:
user selects settings, then presses start button.
gui displays question and textfield for user input.
user inputs answer within X amount of seconds or gui automatically move onto the next question - alternatively, user can move onto next question immediately by pressing next button.
GUI displays score and average.
The problems is getText() from user textfield is processed as soon as start button is pressed, without giving the user a chance to enter an answer. How do I make the program wait for X amount of seconds or for the next button to be clicked before processing the user's answer? Here's my code:
//start button changes view and then runs startTest()
start.setOnAction(e -> {
setLeft(null);
setRight(null);
setCenter(test_container);
running_program_title.setText(getDifficulty().name() + " Test");
buttons_container.getChildren().clear();
buttons_container.getChildren().addAll(next, quit, submit);
startTest();
});
Here is the problem code... at least how I see it.
//startTest method calls askAdd() to ask an addition question
void startTest() {
int asked = 0;
int correct = 0;
while (asked < numberOfQuestions) {
if(askAdd()){
correct++;
asked++;
}
}
boolean askAdd() {
int a = (int) (Math.random() * getMultiplier());
int b = (int) (Math.random() * getMultiplier());
//ask question
question.setText("What is " + a + " + " + b + "?");
//code needed to pause method and wait for user input for X seconds
//retrieve user answer and return if its correct
return answer.getText().equalsIgnoreCase(String.valueOf(a+b));
}
I've tried using Thread.sleep(X) but that freezes the gui for however long I specify and then goes through the addAsk() method and the loop before going to the test screen. (I know because I had the program set up to print the questions and answer input to the console). It shows the last question and that's all.
I didn't include the next button code because I can't get the gui to go to the test page anyway.
Any help on any of the code is appreciated.
This can be achieved by various methods.
PauseTransition is one of the many apt solution present. It waits for X time interval and then performs a Task. It can start, restart, stop at any moment.
Here is an example of how it can used to achieve a similar result.
Complete Code
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.stream.IntStream;
public class Main extends Application {
int questionIndex = 0;
int noOfQuestions = 10;
#Override
public void start(Stage stage) {
VBox box = new VBox(10);
box.setPadding(new Insets(10));
Scene scene = new Scene(new ScrollPane(box), 500, 200);
ObservableList<String> questions =
FXCollections.observableArrayList("1) Whats your (full) name?",
"2) How old are you?",
"3) Whats your Birthday?",
"4) What starsign does that make it?",
"5) Whats your favourite colour?",
"6) Whats your lucky number?",
"7) Do you have any pets?",
"8) Where are you from?",
"9) How tall are you?",
"10) What shoe size are you?");
ObservableList<String> answers = FXCollections.observableArrayList();
final PauseTransition pt = new PauseTransition(Duration.millis(5000));
Label questionLabel = new Label(questions.get(questionIndex));
Label timerLabel = new Label("Time Remaining : ");
Label time = new Label();
time.setStyle("-fx-text-fill: RED");
TextField answerField = new TextField();
Button nextQuestion = new Button("Next");
pt.currentTimeProperty().addListener(new ChangeListener<Duration>() {
#Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
time.setText(String.valueOf(5 - (int)newValue.toSeconds()));
}
});
box.getChildren().addAll(questionLabel, answerField, new HBox(timerLabel, time), nextQuestion);
nextQuestion.setOnAction( (ActionEvent event) -> {
answers.add(questionIndex, answerField.getText());
//Check if it is the last question
if(questionIndex == noOfQuestions-1) {
pt.stop();
box.getChildren().clear();
IntStream.range(0, noOfQuestions).forEach(i -> {
Label question = new Label("Question : " + questions.get(i));
question.setStyle("-fx-text-fill: RED");
Label answer = new Label("Answer : " + answers.get(i));
answer.setStyle("-fx-text-fill: GREEN");
box.getChildren().addAll(question, answer);
});
}
// All other time
else {
//Set new question
questionLabel.setText(questions.get(++questionIndex));
answerField.clear();
pt.playFromStart();
}
});
pt.setOnFinished( ( ActionEvent event ) -> {
nextQuestion.fire();
});
pt.play();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
For the timer you should (IMO) use a Timeline. Here is an example:
public class MultiGame extends Application {
ProgressBar progressBar;
final int allowedTime = 5; //seconds
final DoubleProperty percentOfTimeUsed = new SimpleDoubleProperty(0);
final Timeline timer =
new Timeline(
new KeyFrame(
Duration.ZERO, new KeyValue(percentOfTimeUsed, 0)),
new KeyFrame(
Duration.seconds(allowedTime), new KeyValue(percentOfTimeUsed, 1))
);
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
progressBar = new ProgressBar();
progressBar.progressProperty().bindBidirectional(percentOfTimeUsed);
root.setTop(progressBar);
Button answer = new Button("Answer");
answer.setOnAction(ae -> restart());// the on answer handler
Button skip = new Button("Skip");
skip.setOnAction(ae -> restart());// the skip question handler
HBox mainContent = new HBox(15,
new Label("Your Question"), new TextField("The answer"), answer, skip);
root.setCenter(mainContent);
timer.setOnFinished(ae -> restart());// the end of timer handler
primaryStage.setScene(new Scene(root));
primaryStage.show();
restart();
}
void restart() { timer.stop(); timer.playFromStart(); }
void pause() { timer.pause(); }
void resume() { timer.play(); }
}
You just need to capture the text from the input in between the starting of the timeline and the restart method.