问题
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
:
-fx-cursor:default;
Has no effect as the cursor remains the text cursor. That is weird as i use the same class for aTextField
with proper/expected resultsThe 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