Issue
Just a quick question which is on my mind right now:
I have a JavaFX application that contains (among others) a ScrollPane and I need to capture the MouseClicked event of that ScrollPane. That itself is actually no problem, except that I only need to handle the event if the event target is an instance of a Rectangle, ToggleButton or a ScrollPaneSkin. That is actually quite easy, too, I know. Right now, I have the following code:
@FXML
void scrollPaneOnMouseClicked(MouseEvent event) {
System.out.println(event.getTarget().getClass().getName());
System.out.println(event.getTarget() instanceof ScrollPaneSkin);
if (event.getTarget() instanceof RoomRectangle || event.getTarget() instanceof ToggleButton || event.getTarget() instanceof ScrollPaneSkin) {
// handle
}
}
except that event.getTarget() instanceof ScrollPaneSkin
says false even if System.out.println(event.getTarget().getClass().getName());
outputs com.sun.javafx.scene.control.skin.ScrollPaneSkin$4
(and the debugger confirms that).
I also tried event.getTarget() instanceof ScrollPaneSkin$4
which resulted in a "cannot find symbol"-error.
What did I miss here?
Solution
Ok, I fixed it by myself. The problem was quite simple and the reason is $4
. As I just found out, the $4
points to an anonymous inner class of ScrollPaneSkin
that obviously cannot be accessed from outside. That means that the target really was not an instance of ScrollPaneSkin
but rather an instance of that inner class. The only workaround is to use event.getTarget.getClass().getName()
and do a String-comparison. (Solution taken from here)
Answered By - vatbub
Answer Checked By - Marilyn (JavaFixing Volunteer)