当TextArea成为Javafx的焦点时,如何打开“文本输入”对话框? [英] How to open Text Input Dialog Box, when a TextArea gets focus in Javafx?

查看:258
本文介绍了当TextArea成为Javafx的焦点时,如何打开“文本输入”对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码,每当文本区域被关注时,都会打开一个文本输入对话框。

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.

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

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

推荐答案

当焦点从对话框返回到主舞台时,文本区域将再次获得焦点,这将再次触发弹出对话框。您可以将焦点从textarea上避开:

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:


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

这篇关于当TextArea成为Javafx的焦点时,如何打开“文本输入”对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆