2

I want to have a ListView with the name of the "Persons", but on the ListView when I launch the application, I see some weird strings instead of the (string)names. I think it is a cast problem of the "Person"-object. I didnt know if the ListView should be ListView or ListView. Because when I try to set the items on the listview, using setItems(ObservableList) i get that weird Strings on list shown.. here is the code:

MainApp.java:

 public static ObservableList<Person> personList;

 public static void main(String[] args) {
    personList = FXCollections.observableArrayList();
    Person p1 = new Person("Tom");
    Person p2 = new Person("Anna");

    personList.add(p1);
    personList.add(p2);
    launch(args);
}

PersonController.java:

import static ordercheck.OrderCheck.personList;

public class PersonalController implements Initializable {
@FXML
private ListView<Person> personalList;
 public void initialize(URL url, ResourceBundle rb) {
   personalList.setItems(personList);
....
....
}

....
....
}

how could I see the real names and not such strings: ordercheck.model.Person@2c5ds8dsf8sdf8

Thank you!

ZelelB
  • 1,836
  • 7
  • 45
  • 71

1 Answers1

2

Either:

a. Override the toString method in your Person class.

OR

b. Set a cellFactory on the ListView and set your formatted text there.

(a) is easiest . . .

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • For more information, as well as an example of (b), see [How can I Populate a ListView in JavaFX using Custom Objects?](https://stackoverflow.com/questions/36657299) (which is also [answered](https://stackoverflow.com/a/36657553/6395627) by jewelsea). – Slaw Feb 19 '19 at 13:26