How do you listen for mouse paste done in a TextField using JavaFX?

我怕爱的太早我们不能终老 提交于 2019-12-08 08:17:33

问题


Copy text into your clipboard, right click text field and press "paste", is there a way how to listen when paste was clicked? Or rather that input text in the field changed after something was pasted this way. Because these do not work in this particular case:

setOnKeyReleased()
setOnInputMethodTextChanged()

回答1:


The "paste" functionality is implemented in the TextInputControl superclass of TextField in public void paste(). So, while it's not really an event-driven or MVC approach, you can react to a "paste" action (whether it's invoked by mouse or keyboard shortcut, typically ctrl-V) by overriding this method:

TextField tf = new TextField() {
    @Override
    public void paste() {
        super.paste();
        System.out.println("text pasted in");
    }
}


来源:https://stackoverflow.com/questions/41762158/how-do-you-listen-for-mouse-paste-done-in-a-textfield-using-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!