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 http://java-buddy.blogspot.com/
 */
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));
    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("http://java-buddy.blogspot.com/");

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

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

                    @Override
                    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();
        root.getChildren().add(listView);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


Next:
- Sort List of custom object
- Customized ListView


Related: Implement ListView using JavaFX

4 comments:

  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.setItems(custObservableList);
    custListView.cellFactoryProperty(new Callback, ListCell>(){
    @Override
    public ListCell call(ListView p) {

    ListCell cell = new ListCell(){

    @Override
    protected void updateItem(Customer custValue, boolean bln) {
    super.updateItem(custValue, bln);
    if (custValue != null) {
    setText(custValue.getName());
    }
    }

    };

    return cell;
    }
    });

    ReplyDelete
    Replies
    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!

      Delete
  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: http://pastebin.com/jf6JCSem

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

    ReplyDelete