Sunday, May 5, 2013

Implement JavaFX ListView for custom object

This example demonstrate how to implement ListView of custom object.

JavaFX ListView for custom object
JavaFX ListView for custom object

Here we define a custom class, MyObject. And a List of object of MyObject class, myList. We have to generate ObservableList<myobject> from ArrayList<myobject> by calling FXCollections.observableList(myList). Also have to sets a new cell factory to use in the ListView, by calling setCellFactory() method.

Here is the example code:

package javafx_listview;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

 * @web
public class JavaFX_ListView extends Application {
    class MyObject{
        String day;
        int number;
        MyObject(String d, int n){
            day = d;
            number = n;
        String getDay(){
            return day;
        int getNumber(){
            return number;
    List<MyObject> myList;
    //Create dummy list of MyObject
    private void prepareMyList(){
        myList = new ArrayList<>();
        myList.add(new MyObject("Sunday", 50));
        myList.add(new MyObject("Monday", 60));
        myList.add(new MyObject("Tuesday", 20));
        myList.add(new MyObject("Wednesday", 90));
        myList.add(new MyObject("Thursday", 30));
        myList.add(new MyObject("Friday", 62));
        myList.add(new MyObject("Saturday", 65));

    public void start(Stage primaryStage) {


        ListView<MyObject> listView = new ListView<>();
        ObservableList<MyObject> myObservableList = FXCollections.observableList(myList);
        listView.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>(){

            public ListCell<MyObject> call(ListView<MyObject> p) {
                ListCell<MyObject> cell = new ListCell<MyObject>(){

                    protected void updateItem(MyObject t, boolean bln) {
                        super.updateItem(t, bln);
                        if (t != null) {
                            setText(t.getDay() + ":" + t.getNumber());

                return cell;

        StackPane root = new StackPane();
        primaryStage.setScene(new Scene(root, 300, 250));;

    public static void main(String[] args) {

- Sort List of custom object
- Customized ListView

Related: Implement ListView using JavaFX


  1. I tried to implement this code in my own version and I got an error. I tried to lookup and understand the error myself, but to no avail. Perhaps you know what would trigger the error.

    method cellFactoryProperty in class ListView cannot be applied to given types;

    ObservableList custObservableList = FXCollections.observableList(dataFile.customerList);
    ListView custListView = new ListView<>();
    custListView.cellFactoryProperty(new Callback, ListCell>(){
    public ListCell call(ListView p) {

    ListCell cell = new ListCell(){

    protected void updateItem(Customer custValue, boolean bln) {
    super.updateItem(custValue, bln);
    if (custValue != null) {


    return cell;

    1. Try to use setCellFactory() method, instead of cellFactoryProperty().

      remark: I think there should be some < symbol removed by blogger, so I can'r view your full code!

  2. I think part of my problem is understanding where the "p" object from "ListView p" is used after it is initialized. I didn't see it anywhere else in the code, so I probably didn't include it where I should have.

    Here is a link to my code:

  3. Simply overriding the toString() method of your object would achieve same result.