1

I have tried the following code, to open a text input dialog box, whenever the textarea gets focused.

TextArea address = new TextArea();
        address.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
                if (newPropertyValue) {
                    System.out.println("Textfield on focus");
                    TextInputDialog dialog = new TextInputDialog("walter");
                    dialog.setTitle("Text Input Dialog");
                    dialog.setHeaderText("Look, a Text Input Dialog");
                    dialog.setContentText("Please enter your name:");
                    // Traditional way to get the response value.
                    Optional<String> result = dialog.showAndWait();
                    if (result.isPresent()) {
                        System.out.println("Your name: " + result.get());
                    }
                } else {
                    System.out.println("Textfield out focus");
                }
            }
        });

But for every single click on Dialog box a new dialog box is opened.

I just want to open the dialog box once (onFocus of textarea), perform some task and close it. please help me out...!!

Vikas Singh
  • 175
  • 5
  • 19

2 Answers2

1

When the focus returns from dialog to main stage the textarea will gain a focus again which will be trigger popping up the dialog again. You can focus out from textarea to avoid this:

address.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
            if (newPropertyValue) {
                System.out.println("Textfield on focus");
                TextInputDialog dialog = new TextInputDialog("walter");
                dialog.setTitle("Text Input Dialog");
                dialog.setHeaderText("Look, a Text Input Dialog");
                dialog.setContentText("Please enter your name:");
                // Traditional way to get the response value.
                Optional<String> result = dialog.showAndWait();
                if (result.isPresent()) {
                    System.out.println("Your name: " + result.get());
                }

                // focus to different node on the scene
                address.getParent().requestFocus();
                // or mySubmitBtn.requestFocus();

            } else {
                System.out.println("Textfield out focus");
            }
        }
    });

MCVE:

@Override
public void start( Stage stage )
{
    TextArea address = new TextArea();
    address.focusedProperty().addListener( new ChangeListener<Boolean>()
    {
        @Override
        public void changed( ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue )
        {
            if ( newPropertyValue )
            {
                System.out.println( "Textfield on focus" );
                TextInputDialog dialog = new TextInputDialog( "walter" );
                dialog.setTitle( "Text Input Dialog" );
                dialog.setHeaderText( "Look, a Text Input Dialog" );
                dialog.setContentText( "Please enter your name:" );
                // Traditional way to get the response value.
                Optional<String> result = dialog.showAndWait();
                if ( result.isPresent() )
                {
                    System.out.println( "Your name: " + result.get() );
                }

                // focus to different node on the scene
                address.getParent().requestFocus();
                // or mySubmitBtn.requestFocus();

            }
            else
            {
                System.out.println( "Textfield out focus" );
            }
        }
    } );

    Scene scene = new Scene( new VBox( address ), 200, 200 );
    stage.setScene( scene );
    stage.show();
}
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • Whenever I click on Dialog box its giving out put as "Textfield out focus". I cannot close the dialog box. I think its in some kind of loop.. – Vikas Singh Sep 29 '15 at 09:02
  • @VikasSingh, By "Whenever I click on Dialog box" you mean, clicking on "Cancel" or "OK" buttons right? Add the `address.getParent().requestFocus();` line exactly as in my answer above, and try again. – Uluk Biy Sep 29 '15 at 09:49
  • I have tried the same code that you have written, but its not working. And Yes, the dialog box is not getting closed by any of the button. – Vikas Singh Sep 29 '15 at 10:15
  • @VikasSingh I added a complete sample code. With this, the dialog is not popping up continuously. Can you also try it and then compare with yours? – Uluk Biy Sep 29 '15 at 10:33
0

I have tried this code and it worked perfectly..!!

 @Override
    public void start(Stage stage) {
        TextArea address = new TextArea();

        address.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
                TextInputDialog dialog = new TextInputDialog("walter");
                dialog.setTitle("Text Input Dialog");
                dialog.setHeaderText("Look, a Text Input Dialog");
                dialog.setContentText("Please enter your name:");

                if (newPropertyValue) {
                    System.out.println("Old Property : " + oldPropertyValue);
                    System.out.println("New Property : " + newPropertyValue);
                    System.out.println("Textfield on focus");
                    address.getParent().requestFocus();
                    Optional<String> result = dialog.showAndWait();
                    System.out.println("Result :" + result);
                    if (result.isPresent()) {
                        dialog.getDialogPane().requestFocus();
                        System.out.println("Your name: " + result.get());
                    }
                }
            }
        });

        Scene scene = new Scene(new VBox(address), 200, 200);
        stage.setScene(scene);
        stage.show();
        address.getParent().requestFocus();
    }
Vikas Singh
  • 175
  • 5
  • 19