#JavaFX Storing Objects with setOnMouseClick() Action
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
want to store them in an ArrayList whenever they are clicked on. Here's how you can achieve this using JavaFX:
- Create a class that extends Circle and add a constructor that sets the event handler for mouse clicks:
public class ClickableCircle extends Circle {
public ClickableCircle() {
setOnMouseClicked(event -> {
// Add the clicked circle to the list
clickedCircles.add(this);
});
}
}
- In your main class, create an ArrayList to store the clicked circles:
ArrayList<ClickableCircle> clickedCircles = new ArrayList<>();
- Create 5 instances of ClickableCircle and add them to a Pane or Group in your JavaFX application:
Pane root = new Pane();
for(int i=0; i<5; i++){
ClickableCircle circle = new ClickableCircle();
circle.setCenterX(50 + 100*i);
circle.setCenterY(50);
circle.setRadius(20);
root.getChildren().add(circle);
}
Now, whenever you click on one of the circles, it will be added to the clickedCircles ArrayList. You can then access this list to perform any further actions with the clicked circles.
we have solved this problem in the past
it sounds like you are overriding the listeners of previous components
have you tried using a debugger to step through your code? thatll help you determine if its actually being overwritten, or if there's a different issue