How can I know if the OK or the Cancel Button was pressed in this JavaFX dialog.
The Dialog Code:
public String delimiter;
public void delimiterYES() throws IOException {
delimiter=new String();
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Delimiter");
dialog.setHeaderText("Enter the delimiter");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
delimiter=result.get();
}
}
If a result is present, then the user pressed OK. If no result is present, then the user probably pressed cancel, but they might have just closed the dialog window using the OS close window function.
Optional<String> result = new TextInputDialog().showAndWait();
if (result.isPresent()) {
// ok was pressed.
} else {
// cancel might have been pressed.
}
To really know if a button was pressed, you can use a filter as noted in the Dialog javadoc section "Dialog Validation / Intercepting Button Actions".
final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("Cancel was definitely pressed")
);
Sample code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.util.Optional;
public class DialogSample extends Application {
#Override
public void start(Stage stage) throws Exception {
Button showButton = new Button("show");
showButton.setOnAction(event -> showDialog(stage));
showButton.setPrefWidth(100);
stage.setScene(new Scene(showButton));
stage.show();
showButton.fire();
}
private void showDialog(Stage stage) {
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.setTitle("Delimiter");
dialog.setHeaderText("Enter the delimiter");
final Button ok = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
ok.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("OK was definitely pressed")
);
final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("Cancel was definitely pressed")
);
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Result present => OK was pressed");
System.out.println("Result: " + result.get());
} else {
System.out.println("Result not present => Cancel might have been pressed");
}
}
public static void main(String[] args) {
Application.launch();
}
}
Ok, I found the answere here JavaFX Dialogs
The result.isPresent() will return false if the user cancelled the dialog.
You can use Optional<ButtonType> instead of Optional<String>. And basically use the below code.
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK){
System.out.println("Ok button is pressed");
} else if(result.isPresent() && result.get() == ButtonType.CANCEL){
System.out.println("Cancel button was pressed");
}
Hope it helps. Let me know if you need any further clarification.
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);
}
}
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 am writing a Java program in which at a certain point, a new directory has to be created. However, there should be an alert message whether the user wants to continue or not. The alert box has to contain the options to
1. Proceed
2. Discontinue
3. Show the targeted directory in Windows explorer.
I have already created an alert confirmation box (the method returns whether the program can proceed to move files to the targeted directory):
private static boolean createDir(Movie movie){
String name = movie.name.getValue();
File file = new File(Paths.get(target.getValue(),name).toString());
if(!file.isDirectory()) {
file.mkdir();
return true;
}
else{
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Duplicate");
alert.setHeaderText("This folder already exists");
alert.setContentText("Do you want to continue (this program will overwrite any files with duplicate names)");
ButtonType show = new ButtonType("Show in Explorer");
alert.getButtonTypes().add(show);
Optional<ButtonType> option = alert.showAndWait();
if (option.get() == null) {
return false;
} else if (option.get() == ButtonType.OK) {
return true;
} else if (option.get() == ButtonType.CANCEL) {
return false;
} else if (option.get() == show) {
try {
Desktop.getDesktop().open(new File(file.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
} else {
return false;
}
}
return false;
}
The problem is, that I want the alert box to stay on screen when the users clicks on "Show in Explorer". The user will look at the already existing folder and then click OK or cancel (or again show in Explorer).
Thanks in advance :)
try this
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Duplicate");
alert.setHeaderText("This folder already exists");
alert.setContentText("Do you want to continue (this program will overwrite any files with duplicate names)");
ButtonType show = new ButtonType("Show in Explorer", ButtonBar.ButtonData.LEFT);
alert.getButtonTypes().add(show);
ButtonBar.setButtonUniformSize(alert.getDialogPane().lookupButton(show), false);
alert.getDialogPane().lookupButton(show).addEventFilter(ActionEvent.ACTION, event -> {
try {
Desktop.getDesktop().open(new File(new File(file.getPath())));
}
catch (IOException e) {
e.printStackTrace();
}
event.consume();
});
Optional<ButtonType> option = alert.showAndWait();
return ButtonType.OK.equals(option.get());
You need to create a regular button for "Show in Explorer", instead of making that one of the buttons that is created by default to correspond to a ButtonType. (The default behavior is to create a button that closes the dialog and sets the value of the dialog's result.)
If you want the convenience of an Alert and are OK with the slightly different UI this gives, you can do something like
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Duplicate");
alert.setHeaderText("This folder already exists");
alert.setContentText("Do you want to continue (this program will overwrite any files with duplicate names)");
Button show = new Button("Show in Explorer");
show.setOnAction(e -> {
try {
Desktop.getDesktop().open(new File(file.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
});
alert.setGraphic(show);
return alert.showAndWait().filter(ButtonType.OK::equals).isPresent();
If you want the button to appear in the button bar, you need to subclass DialogPane and override the createButton method to return a button that performs the action you want.
Here's a complete example using this approach:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class AlertWithRegularButton extends Application {
#Override
public void start(Stage primaryStage) {
Button save = new Button("Save");
save.setOnAction(e -> showDialog(primaryStage));
StackPane root = new StackPane(save);
Scene scene = new Scene(root, 250, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
private void showDialog(Stage owner) {
ButtonType showFileBrowserType = new ButtonType("Show in system file browser");
DialogPane dialogPane = new DialogPane() {
#Override
protected Node createButton(ButtonType type) {
if (type == showFileBrowserType) {
Button show = new Button(type.getText());
show.setOnAction(e -> {
try {
File home = new File(System.getProperty("user.home"));
Desktop.getDesktop().open(home);
} catch (IOException exc) {
exc.printStackTrace();
}
});
return show ;
} else {
return super.createButton(type);
}
}
};
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setDialogPane(dialogPane);
alert.setTitle("Duplicate");
alert.setHeaderText("This folder already exists");
alert.setContentText("Do you want to continue (this program will overwrite any files with duplicate names)");
alert.getButtonTypes().addAll(showFileBrowserType, ButtonType.CANCEL, ButtonType.OK);
alert.initOwner(owner);
alert.showAndWait().filter(ButtonType.OK::equals).ifPresent(b -> {
System.out.println("OK chosen");
});
}
public static void main(String[] args) {
launch(args);
}
}
I have a simple login window in JavaFX. When the user inserts his username and password I want to make a simple string "progress bar" in another thread while the main thread processes the inputs.
When the main thread gets to the if statement (let's say the passwords don't match) I want the progress to stop when the alert is thrown. But with this code it continues even after the alert is thrown.
public void validateLogin(ActionEvent actionEvent) throws Exception {
Thread thread = new Thread(() -> {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(loading_txt.getText().length() < 10)loading_txt.setText(loading_txt.getText() + "|");
else loading_txt.setText("|");
}
});
thread.start();
String username = username_field.getText();
String password = password_field.getText();
if (!(BCrypt.checkpw(password_field.getText(), dbHandler.getLoginByUsername(username_field.getText()).getPassword()))) {
throwAlert(Alert.AlertType.ERROR,"Login problem", "Password doesn't match.", "Wrong password. Please, check out and try it again. ");
thread.join();
return;
}
thread.join();
//other code
}
So I made a little change in the if statement and put the thread.join() before the alert. Now the progress can't be even seen.
if (!(BCrypt.checkpw(password_field.getText(), dbHandler.getLoginByUsername(username_field.getText()).getPassword()))) {
thread.join();
throwAlert(Alert.AlertType.ERROR,"Login problem", "Password doesn't match.", "Wrong password. Please, check out and try it again. ");
return;
}
How does this little change cause the progress to be seen or not to be seen? What do I have to change to stop the progressing when the alert is thrown? Could it be caused by some functionality in JavaFX?
Here is an Example, you may take the idea and apply it to your program (Explanation in comments).
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.ProgressBar;
public class ProgressBarExample extends Application{
ProgressBar pb = new ProgressBar(); // your progress bar
#Override
public void start(Stage primaryStage) throws Exception {
// The structure and components are for example only
TextField password = new TextField();
Button test = new Button("Test");
HBox container = new HBox();
container.getChildren().addAll(password, test);
container.setAlignment(Pos.CENTER);
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(container);
// add action listener to the button
test.setOnAction(e->{
// when it's pressed add Progress bar and other stuff that are concerned with the GUI!
Platform.runLater(new Runnable(){ // always use this to update GUI components
#Override
public void run() {
root.getChildren().add(pb);
// you can add label to the root...etc
// or update your progress bar ..etc
// in a nutshell: anything needs to be updated in GUI.
}
});
Task<Boolean> validatePassword = new Task<Boolean>(){ // always use Task to do complex-long calculations
#Override
protected Boolean call() throws Exception {
return validatePassword(password); // method to validate password (see later)
}
};
validatePassword.setOnSucceeded(ee->{ // when Task finishes successfully
System.out.println("Finished");
root.getChildren().remove(pb); // remove the progress bar
if(!validatePassword.getValue()){
Alert alert = new Alert(AlertType.ERROR, "Wrong Password", ButtonType.OK);
alert.showAndWait();
}
});
validatePassword.setOnFailed(eee->{ // if it fails
System.out.println("Failed");
root.getChildren().remove(pb); // remove it anyway
});
new Thread(validatePassword).start(); // add the task to a thread and start it
});
Scene scene = new Scene(root, 300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
// validate here in this method
public static boolean validatePassword(TextField password){
for(int i=0; i<99999; i++){ // suppose it is a long process
System.out.println("Processing");
}
if(password.getText().equalsIgnoreCase("Invalid")){ // suppose it's invalid, just for testing
return false
}
return true;
}
public static void main(String[] args) {
launch();
}
}
Test:
I would like to know if it was possible to detect the double-click in JavaFX 2 ? and how ?
I would like to make different event between a click and a double click.
Thanks
Yes you can detect single, double even multiple clicks:
myNode.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
if(mouseEvent.getClickCount() == 2){
System.out.println("Double clicked");
}
}
}
});
MouseButton.PRIMARY is used to determine if the left (commonly) mouse button is triggered the event. Read the api of getClickCount() to conclude that there maybe multiple click counts other than single or double. However I find it hard to distinguish between single and double click events. Because the first click count of the double click will rise a single event as well.
Here is another piece of code which can be used if you have to distinguish between a single- and a double-click and have to take a specific action in either case.
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class DoubleClickDetectionTest extends Application {
boolean dragFlag = false;
int clickCounter = 0;
ScheduledThreadPoolExecutor executor;
ScheduledFuture<?> scheduledFuture;
public DoubleClickDetectionTest() {
executor = new ScheduledThreadPoolExecutor(1);
executor.setRemoveOnCancelPolicy(true);
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
if (e.getButton().equals(MouseButton.PRIMARY)) {
dragFlag = true;
}
}
});
root.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
if (e.getButton().equals(MouseButton.PRIMARY)) {
if (!dragFlag) {
System.out.println(++clickCounter + " " + e.getClickCount());
if (e.getClickCount() == 1) {
scheduledFuture = executor.schedule(() -> singleClickAction(), 500, TimeUnit.MILLISECONDS);
} else if (e.getClickCount() > 1) {
if (scheduledFuture != null && !scheduledFuture.isCancelled() && !scheduledFuture.isDone()) {
scheduledFuture.cancel(false);
doubleClickAction();
}
}
}
dragFlag = false;
}
}
});
}
#Override
public void stop() {
executor.shutdown();
}
private void singleClickAction() {
System.out.println("Single-click action executed.");
}
private void doubleClickAction() {
System.out.println("Double-click action executed.");
}
}
Adhering to Java SE 8 lambda expressions would look something like this:
node.setOnMouseClicked(event -> {
if(event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {
handleSomeAction();
}
});
Once you get used to lambda expressions - they end up being more understandable than the original class instantiation and overriding (x) method. -In my opinion-
The response by P. Pandey is the simplest approach which actually distinguishes between single and double click, but it did not work for me. For one, the function "currentTimeMillis" already returns milliseconds, so dividing it by 1000 does not seem to be necessary. The version below worked for me in a more consistent fashion.
#Override
public void handle(MouseEvent t) {
long diff = 0;
currentTime=System.currentTimeMillis();
if(lastTime!=0 && currentTime!=0){
diff=currentTime-lastTime;
if( diff<=215)
isdblClicked=true;
else
isdblClicked=false;
}
lastTime=currentTime;
System.out.println("IsDblClicked()"+isdblClicked);
//use the isdblClicked flag...
}
Not sure if someone still follows this OP or refer it, but below is my version of differentiating single click to double click. While most of the answers are quite acceptable, it would be really useful if it can be done in a proper resuable way.
One of the challenge I encountered is the need to have the single-double click differentiation on multiple nodes at multiple places. I cannot do the same repetitive cumbersome logic on each and every node. It should be done in a generic way.
So I opted to implement a custom EventDispatcher and use this dispatcher on node level or I can apply it directly to Scene to make it applicable for all child nodes.
For this I created a new MouseEvent namely 'MOUSE_DOUBLE_CLICKED", so tthat I am still sticking with the standard JavaFX practises. Now I can include the double_clicked event filters/handlers just like other mouse event types.
node.addEventFilter(CustomMouseEvent.MOUSE_DOUBLE_CLICKED, e->{..<code to handle double_click>..});
node.addEventHandler(CustomMouseEvent.MOUSE_DOUBLE_CLICKED, e->{..<code to handle double_click>..});
Below is the implementation and complete working demo of this custom event dispatcher.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class DoubleClickEventDispatcherDemo extends Application {
#Override
public void start(Stage stage) throws Exception {
Rectangle box1 = new Rectangle(150, 150);
box1.setStyle("-fx-fill:red;-fx-stroke-width:2px;-fx-stroke:black;");
addEventHandlers(box1, "Red Box");
Rectangle box2 = new Rectangle(150, 150);
box2.setStyle("-fx-fill:yellow;-fx-stroke-width:2px;-fx-stroke:black;");
addEventHandlers(box2, "Yellow Box");
HBox pane = new HBox(box1, box2);
pane.setSpacing(10);
pane.setAlignment(Pos.CENTER);
addEventHandlers(pane, "HBox");
Scene scene = new Scene(new StackPane(pane), 450, 300);
stage.setScene(scene);
stage.show();
// SETTING CUSTOM EVENT DISPATCHER TO SCENE
scene.setEventDispatcher(new DoubleClickEventDispatcher(scene.getEventDispatcher()));
}
private void addEventHandlers(Node node, String nodeId) {
node.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> System.out.println("" + nodeId + " mouse clicked filter"));
node.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> System.out.println("" + nodeId + " mouse clicked handler"));
node.addEventFilter(CustomMouseEvent.MOUSE_DOUBLE_CLICKED, e -> System.out.println("" + nodeId + " mouse double clicked filter"));
node.addEventHandler(CustomMouseEvent.MOUSE_DOUBLE_CLICKED, e -> System.out.println(nodeId + " mouse double clicked handler"));
}
/**
* Custom MouseEvent
*/
interface CustomMouseEvent {
EventType<MouseEvent> MOUSE_DOUBLE_CLICKED = new EventType<>(MouseEvent.ANY, "MOUSE_DBL_CLICKED");
}
/**
* Custom EventDispatcher to differentiate from single click with double click.
*/
class DoubleClickEventDispatcher implements EventDispatcher {
/**
* Default delay to fire a double click event in milliseconds.
*/
private static final long DEFAULT_DOUBLE_CLICK_DELAY = 215;
/**
* Default event dispatcher of a node.
*/
private final EventDispatcher defaultEventDispatcher;
/**
* Timeline for dispatching mouse clicked event.
*/
private Timeline clickedTimeline;
/**
* Constructor.
*
* #param initial Default event dispatcher of a node
*/
public DoubleClickEventDispatcher(final EventDispatcher initial) {
defaultEventDispatcher = initial;
}
#Override
public Event dispatchEvent(final Event event, final EventDispatchChain tail) {
final EventType<? extends Event> type = event.getEventType();
if (type == MouseEvent.MOUSE_CLICKED) {
final MouseEvent mouseEvent = (MouseEvent) event;
final EventTarget eventTarget = event.getTarget();
if (mouseEvent.getClickCount() > 1) {
if (clickedTimeline != null) {
clickedTimeline.stop();
clickedTimeline = null;
final MouseEvent dblClickedEvent = copy(mouseEvent, CustomMouseEvent.MOUSE_DOUBLE_CLICKED);
Event.fireEvent(eventTarget, dblClickedEvent);
}
return mouseEvent;
}
if (clickedTimeline == null) {
final MouseEvent clickedEvent = copy(mouseEvent, mouseEvent.getEventType());
clickedTimeline = new Timeline(new KeyFrame(Duration.millis(DEFAULT_DOUBLE_CLICK_DELAY), e -> {
Event.fireEvent(eventTarget, clickedEvent);
clickedTimeline = null;
}));
clickedTimeline.play();
return mouseEvent;
}
}
return defaultEventDispatcher.dispatchEvent(event, tail);
}
/**
* Creates a copy of the provided mouse event type with the mouse event.
*
* #param e MouseEvent
* #param eventType Event type that need to be created
* #return New mouse event instance
*/
private MouseEvent copy(final MouseEvent e, final EventType<? extends MouseEvent> eventType) {
return new MouseEvent(eventType, e.getSceneX(), e.getSceneY(), e.getScreenX(), e.getScreenY(),
e.getButton(), e.getClickCount(), e.isShiftDown(), e.isControlDown(), e.isAltDown(),
e.isMetaDown(), e.isPrimaryButtonDown(), e.isMiddleButtonDown(),
e.isSecondaryButtonDown(), e.isSynthesized(), e.isPopupTrigger(),
e.isStillSincePress(), e.getPickResult());
}
}
}
Here is how I have implemented double click
if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
long diff = 0;
if(time1==0)
time1=System.currentTimeMillis();
else
time2=System.currentTimeMillis();
if(time1!=0 && time2!=0)
diff=time2-time1;
if((diff/1000)<=215 && diff>0)
{
isdblClicked=true;
}
else
{
isdblClicked=false;
}
System.out.println("IsDblClicked()"+isdblClicked);
}
Since it is not possible to distinguish between single-click and double-click by default, we use the following approach:
On single-click, we wrap the single-click operation in an abortable runnable. This runnable waits a certain amount of time (i.e., SINGLE_CLICK_DELAY) before being executed.
In the meantime, if a second click, i.e., a double-click, occurs, the single-click operation gets aborted and only the double-click operation is performed.
This way, either the single-click or the double-click operation is performed, but never both.
Following is the full code. To use it, only the three TODO lines have to be replaced by the wanted handlers.
private static final int SINGLE_CLICK_DELAY = 250;
private ClickRunner latestClickRunner = null;
private class ClickRunner implements Runnable {
private final Runnable onSingleClick;
private boolean aborted = false;
public ClickRunner(Runnable onSingleClick) {
this.onSingleClick = onSingleClick;
}
public void abort() {
this.aborted = true;
}
#Override
public void run() {
try {
Thread.sleep(SINGLE_CLICK_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!aborted) {
System.out.println("Execute Single Click");
Platform.runLater(() -> onSingleClick.run());
}
}
}
private void init() {
container.setOnMouseClicked(me -> {
switch (me.getButton()) {
case PRIMARY:
if (me.getClickCount() == 1) {
System.out.println("Single Click");
latestClickRunner = new ClickRunner(() -> {
// TODO: Single-left-click operation
});
CompletableFuture.runAsync(latestClickRunner);
}
if (me.getClickCount() == 2) {
System.out.println("Double Click");
if (latestClickRunner != null) {
System.out.println("-> Abort Single Click");
latestClickRunner.abort();
}
// TODO: Double-left-click operation
}
break;
case SECONDARY:
// TODO: Right-click operation
break;
default:
break;
}
});
}
A solution using PauseTransition:
PauseTransition singlePressPause = new PauseTransition(Duration.millis(500));
singlePressPause.setOnFinished(e -> {
// single press
});
node.setOnMousePressed(e -> {
if (e.isPrimaryButtonDown() && e.getClickCount() == 1) {
singlePressPause.play();
}
if (e.isPrimaryButtonDown() && e.getClickCount() == 2) {
singlePressPause.stop();
// double press
}
});
An alternative to single click vs. double click that I'm using is single click vs. press-and-hold (for about a quarter to a half second or so), then release the button. The technique can use a threaded abortable timer as in some of the code snippets above to distinguish between the two. Assuming that the actual event handling happens on the button release, this alternative has the advantage that single click works normally (i.e., without any delay), and for press-and-hold you can give the user some visual feedback when the button has been held long enough to be released (so there's never any ambiguity about which action was performed).
If you are testing how many mouse buttons (==2) are pressed, do not code it in sub-method! The next is working:
listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if( mouseEvent.getButton().equals(MouseButton.SECONDARY)) {
System.out.println("isSecondaryButtonDown");
mouseEvent.consume();
// ....
}
else
if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
if(mouseEvent.getClickCount() == 2){
System.out.println("Double clicked");
// mousePressedInListViewDC(mouseEvent);
}
else
if(mouseEvent.getClickCount() == 1){
System.out.println("1 clicked");
mousePressedInListView1C(mouseEvent);
}
}
}
})
;
I ran in the same problem, and what I noticed is that single and double click ARE distinguished with basic :
Button btn = new Button("Double click me too");
btn.setOnMousePressed(mouseEvent -> {
// CLICK catches
if (mouseEvent.getClickCount() == 1) {
System.out.println("Button clicked");
} else if (mouseEvent.getClickCount() == 2)
System.out.println("Button double clicked");
});
But a 'single' click is catched as part of the double click. So you will see on the console :
Using mainly the answer of #markus-weninger, I built up a Class extending Button to expose 2 new EventHandlers :
setOnMouseSingleClicked(EventHandler<MouseEvent> eventHandler)
setOnMouseDoubleClicked(EventHandler<MouseEvent> eventHandler)
So with the full example code bellow, when double clicking on last button, we get :
Keep in mind :
The obvious drawback is that even a single click caught with setOnMouseSingleClicked will be delayed with the singleClickDelayMillis (exposed variable which should be set accordingly to the OS, as mentioned by Kleopatra).
Another noticeable fact, is that I extended Button, and not Node where it should be : The Class where the onMouseClicked(...) is implemented.
As a last comment, I decided to add a new EventHandler rather than using the existing setOnMousePressed, setOnMouseReleased or setOnMouseClicked so that the developer can still fully implement these convenience EventHandlers. For example in order to have immediate response from a click on the button without waiting for the singleClickDelayMillis. But this means that if you implement both, the setOnMouseClicked will be fired even on a double click... beware.
Here comes the code :
import java.util.concurrent.CompletableFuture;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.beans.property.ObjectProperty;
import javafx.event.EventHandler;
import javafx.beans.property.SimpleObjectProperty;
public class DblClickCatchedWithoutSingleClick extends Application {
public class ButtonWithDblClick extends Button {
private long singleClickDelayMillis = 250;
private ClickRunner latestClickRunner = null;
private ObjectProperty<EventHandler<MouseEvent>> onMouseSingleClickedProperty = new SimpleObjectProperty<>();
private ObjectProperty<EventHandler<MouseEvent>> onMouseDoubleClickedProperty = new SimpleObjectProperty<>();
// CONSTRUCTORS
public ButtonWithDblClick() {
super();
addClickedEventHandler();
}
public ButtonWithDblClick(String text) {
super(text);
addClickedEventHandler();
}
public ButtonWithDblClick(String text, Node graphic) {
super(text, graphic);
addClickedEventHandler();
}
private class ClickRunner implements Runnable {
private final Runnable onClick;
private boolean aborted = false;
public ClickRunner(Runnable onClick) {
this.onClick = onClick;
}
public void abort() {
this.aborted = true;
}
#Override
public void run() {
try {
Thread.sleep(singleClickDelayMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!aborted) {
Platform.runLater(onClick::run);
}
}
}
private void addClickedEventHandler() {
//Handling the mouse clicked event (not using 'onMouseClicked' so it can still be used by developer).
EventHandler<MouseEvent> eventHandler = me -> {
switch (me.getButton()) {
case PRIMARY:
if (me.getClickCount() == 1) {
latestClickRunner = new ClickRunner(() -> {
System.out.println("ButtonWithDblClick : SINGLE Click fired");
onMouseSingleClickedProperty.get().handle(me);
});
CompletableFuture.runAsync(latestClickRunner);
}
if (me.getClickCount() == 2) {
if (latestClickRunner != null) {
latestClickRunner.abort();
}
System.out.println("ButtonWithDblClick : DOUBLE Click fired");
onMouseDoubleClickedProperty.get().handle(me);
}
break;
case SECONDARY:
// Right-click operation. Not implemented since usually no double RIGHT click needs to be caught.
break;
default:
break;
}
};
//Adding the event handler
addEventHandler(MouseEvent.MOUSE_CLICKED, eventHandler);
}
public void setOnMouseSingleClicked(EventHandler<MouseEvent> eventHandler) {
this.onMouseSingleClickedProperty.set(eventHandler);
}
public void setOnMouseDoubleClicked(EventHandler<MouseEvent> eventHandler) {
this.onMouseDoubleClickedProperty.set(eventHandler);
}
public long getSingleClickDelayMillis() {
return singleClickDelayMillis;
}
public void setSingleClickDelayMillis(long singleClickDelayMillis) {
this.singleClickDelayMillis = singleClickDelayMillis;
}
}
public void start(Stage stage) {
VBox root = new VBox();
Label lbl = new Label("Double click me");
lbl.setOnMouseClicked(mouseEvent -> {
// CLICK catches
if (mouseEvent.getClickCount() == 2) {
System.out.println("Label double clicked");
} else if (mouseEvent.getClickCount() == 1)
System.out.println("Label clicked");
});
Button btn = new Button("Double click me too");
btn.setOnMousePressed(mouseEvent -> {
// CLICK catches
if (mouseEvent.getClickCount() == 1) {
System.out.println("Button clicked");
} else if (mouseEvent.getClickCount() == 2)
System.out.println("Button double clicked");
});
ButtonWithDblClick btn2 = new ButtonWithDblClick("Double click me three ;-)");
btn2.setOnMouseSingleClicked(me -> {
System.out.println("BUTTON_2 : Fire SINGLE Click");
});
btn2.setOnMouseDoubleClicked(me -> {
System.out.println("BUTTON_2 : Fire DOUBLE Click");
});
root.getChildren().add(lbl);
root.getChildren().add(btn);
root.getChildren().add(btn2);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}