I want to create a mouselistner on my javafx rectangle.
the idea is that the rectangle has to change color when i press it?
Does anyone know how to add a listner to shapes in Javafx?
so far ive tried this:
final Rectangle rect = new Rectangle();
rect.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
// TODO Auto-generated method stub
}
});
However i get an error saying that
the method setOnMouseClicked(new EventHandler(){}) is
undefined for the type Rectangle
Abit more information:
The only options i have for rect are these:
rect.add()
rect.contains()
rect.grow();
rect.hashcode()
rect.intersection();
and a few others of no importance.
The import i am using are the following:
import com.sun.glass.events.MouseEvent;
import com.sun.javafx.geom.Rectangle;
import com.sun.javafx.geom.Shape;
Your code looks correct and matches any examples I can find. To demonstrate this I have thrown together a quick example:
When the rectangle is clicked, the colour changes from blue to red.
This might be a long shot but make sure you are referencing the
Rectangletype from theJavaFXlibrary and not theAWTRectangle i.e. make sure your import is:and not
Update
As per my original comment it looks as though you are referencing the wrong import for the
Rectangletype. I don’t recognise the importcom.sun.javafx.geom.Rectangle, is this from an older version of JavaFX?You are also referencing the incorrect
MouseEventtype.Change:
To: