title: JavaFX --- How can I access (and change) the properties of an object in my observable array list?
body: <p>I currently have three classes in my JavaFX program which work together to display Balls (which are stored in an observable array list) onto my interface. Right now, the interface outputs exactly what I want it to (the three balls with correct positioning, size, color etc. </p>
  
  <p>However, I seem to be unable to change the properties using the getters and setters that I implemented, such as x and y position, color, radius etc. It has three Balls on a background. I would like to know how I can access the properties.</p>
  
  <p><code>Ball</code> is a class, which is represented by a Circle <code>circleRepresentation</code> (since Ball is not a Node that is directly able to be displayed on my Pane).</p>
  
  <pre><code>package application;
  
  
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Circle;
  
  public class Ball {
      private String name;
      pivate int xPosition;
      private int yPosition;
      private int radius;
      Color color;
      private Circle circleRepresentation;
  
      public Ball(String name, int xPosition, int yPosition, int radius, Color color) {
          this.name = name;
          this.xPosition = xPosition;
          this.yPosition = yPosition;
          this.radius = radius;
          this.color = color;
          circleRepresentation = new Circle(xPosition, yPosition, radius, color);
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getxPosition() {
          return xPosition;
      }
  
      public void setxPosition(int xPosition) {
          this.xPosition = xPosition;
      }
  
      public int getyPosition() {
          return yPosition;
      }
  
      public void setyPosition(int yPosition) {
          this.yPosition = yPosition;
      }
  
      public int getRadius() {
          return radius;
      }
  
      public void setRadius(int radius) {
          this.radius = radius;
      }
  
  
      public Color getColor() {
          return color;
      }
  
      public void setColor(Color color) {
          this.color = color;
      }
  
      public Circle getCircle() {
          return circleRepresentation;
      }
  
  }
  </code></pre>
  
  <p>I have a <code>BallManager</code>, which stores all the Balls I create in my Main method into an ObservableList called <code>elements</code>.</p>
  
  <pre><code>package application;
  
  import javafx.collections.FXCollections;
  import javafx.collections.ListChangeListener;
  import javafx.collections.ObservableList;
  import javafx.scene.layout.Pane;
  import javafx.scene.shape.Circle;
  
  public class BallManager {
  
      private Pane  group = new Pane ();
      private ObservableList&lt;Ball&gt; elements = FXCollections.observableArrayList();
  
      public BallManager() {
          elements.addListener(new ListChangeListener&lt;Ball&gt;() {
  
              @Override
              public void onChanged(javafx.collections.ListChangeListener.Change&lt;? extends Ball&gt; c) {
                  while (c.next()) {
                      for (Ball remitem : c.getRemoved()) {
                          group.getChildren().remove(remitem.getCircle());
                      }
                      for (Ball additem : c.getAddedSubList()) {
                          Circle circle = additem.getCircle();
  
                          group.getChildren().add(circle);
  
                          circle.relocate(additem.getxPosition(), additem.getyPosition());
                      }
                  }
              }
          });
      }
  
  
  
      public ObservableList&lt;Ball&gt; getElements() {
          return elements;
      }
  
      public Pane getPane() {
          return group;
      }
  }
  </code></pre>
  
  <p>My main method currently allows all of my elements to be outputted on my interface as imagined when the elements are added. However later when I try and set the color of my first ball <code>ball1</code> to white,  with <code>bm.getElements().get(0).setColor(Color.WHITE);</code> it does not allow it.</p>
  
  <pre><code>package application;
  
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;
  
  
  public class Main extends Application {
  
  
      @Override
      public void start(Stage primaryStage) {
          try {
  
              BorderPane root = new BorderPane();
              final Scene scene = new Scene(root, 500, 500);
  
              primaryStage.setTitle("Boucing Balls");
              primaryStage.setScene(scene);
              primaryStage.show();
              scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  
              BallManager bm = new BallManager();
  
              bm.getElements().add(new Ball("ball1", 50, 250, 30, Color.BLUE));
              bm.getElements().add(new Ball("ball2", 250, 50, 30, Color.PLUM));
              bm.getElements().add(new Ball("ball3", 250, 250, 30, Color.AQUAMARINE));
  
              bm.getPane().setStyle("-fx-background-color: orange");
  
              root.setCenter(bm.getPane());
  
              //failed attempt to change the color of ball1, which i believe is at index 0.
              bm.getElements().get(0).setColor(Color.WHITE);
  
              // this statement now prints 'ball1' as intended.
              System.out.println(bm.getElements().get(0).getName());
  
  
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>Any suggestions are welcome, and if I need to include more information, then please tell me in the comments below. Thanks.</p>