TextArea does not handle MouseEvent.MOUSE_PRESSED

霸气de小男生 提交于 2019-12-24 03:20:35

问题


I am building a JavaFX application and I have a TextArea inserted.
The TextArea has a CSS class assigned (don't know if it matters):

.default-cursor{
    -fx-background-color:#EEEEEE;
    -fx-cursor:default;
}


There are 2 issues about this TextArea:

  1. -fx-cursor:default; Has no effect as the cursor remains the text cursor. That is weird as i use the same class for a TextField with proper/expected results
  2. The TextArea does not handle MOUSE_PRESSED event
    My code is :

    textArea.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { System.out.println("print message"); } });


Any ideas Why?
I want to note that when I changed EventHanler to handle MOUSE_CLICKED everything is fine


回答1:


I suspect the default handlers for mouse events on the TextArea are consuming the mouse pressed event before it gets to your handler.

Install an EventFilter instead:

textArea.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("mouse pressed");
            }
        });

The Event filter will get processed before the default handlers see the event.

For your css issue, try

.default-cursor .content {
  -fx-cursor: default ;
}


来源:https://stackoverflow.com/questions/22348152/textarea-does-not-handle-mouseevent-mouse-pressed

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