当TextArea在Javafx中获得焦点时,如何打开文本输入对话框?

时间:2015-09-29 07:56:59

标签: javafx dialog textarea

每当textarea聚焦时,我都尝试了以下代码来打开文本输入对话框。

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");
                }
            }
        });

但是,每次单击“对话框”框都会打开一个新对话框。

我只想打开一次对话框(onTeocus of textarea),执行一些任务并关闭它。 请帮帮我...... !!

2 个答案:

答案 0 :(得分:1)

当焦点从对话框返回到主舞台时,textarea将再次获得焦点,这将触发再次弹出对话框。你可以从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");
            }
        }
    });

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

答案 1 :(得分:0)

我已经尝试过这段代码而且效果很好.. !!

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