How to open Text Input Dialog Box, when a TextArea

2019-08-14 05:15发布

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条回答
小情绪 Triste *
2楼-- · 2019-08-14 05:48

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();
    }
查看更多
We Are One
3楼-- · 2019-08-14 05:55

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();
}
查看更多
登录 后发表回答