0

I have made a list where I place animals in. But when I want them in my ListView I get a weird notation??? What am I missing?

public void AddAnimal(){
    Animal newAnimal = new Animal(getType(), getName(), getGender());
    animals.add(newAnimal);
    AnimalList.setItems(animals);
}

My GUI

enter image description here

Arnaud
  • 17,229
  • 3
  • 31
  • 44
Tom van den Bogaart
  • 143
  • 1
  • 4
  • 15
  • 1
    Contrary to the given answers and [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/), I suggest you _do not_ override `toString()` to fix this. Instead, set the [`cellFactory`](https://openjfx.io/javadoc/11/javafx.controls/javafx/scene/control/ListView.html#setCellFactory(javafx.util.Callback)) property and return a `ListCell` that overrides [`updateItem(Object,boolean)`](https://openjfx.io/javadoc/11/javafx.controls/javafx/scene/control/Cell.html#updateItem(T,boolean)). – Slaw Feb 19 '19 at 13:17
  • Isn't that just the standard Java object address? – Adrian M. Feb 19 '19 at 13:19

2 Answers2

1

You are getting the Animal's native toString string as it's display in the list. If you want to get something else, like the animal's name, you need to override the toString method in Animal class and make it return whatever you want, this.name in case of name.

Ivan Kukic
  • 425
  • 4
  • 14
0

Like Ivan mentioned it you need to Override the default toString() method of the Animal Class to define your own behavior when an Object of the class gets printed:

@Override
public String toString(){
   return getName();
}
main.c
  • 169
  • 2
  • 15