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...!!

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

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..
@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.
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.
@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?
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();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.