#JavaFX Storing Objects with setOnMouseClick() Action

1 messages ยท Page 1 of 1 (latest)

quick ruinBOT
#

<@&987246487241105418> please have a look, thanks.

#

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>.

#
TJ-Bot
I have a class extends Circle. When I create an object of it, I setOnMouseClick() to that object (circle). I create 5 instances of that object and put them all to an ArrayList. The latest created circle instances, overrides all of the listeners of other ci

want to store them in an ArrayList whenever they are clicked on. Here's how you can achieve this using JavaFX:

  1. 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);
        });
    }
    
}
  1. In your main class, create an ArrayList to store the clicked circles:
ArrayList<ClickableCircle> clickedCircles = new ArrayList<>();
  1. 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.

silver blade
#

we have solved this problem in the past

#

it sounds like you are overriding the listeners of previous components

silver blade
#

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