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 EventHandler to handle MOUSE_CLICKED everything is fine

有帮助吗?

解决方案

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 ;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top