Get ListCell via ListView


Question

I have a ListView with my own ListCell<MyObject> implementation. Via a network signal, I receive an index of my ListCell that should be changed.

Over listView.getItems().get(index); there is no problem to access the model, but I want to make a layout change to the listCell with the received index and a layout change to the ListCell with the index+1;

How can I access the ListCell via the ListView?

I search for a method like this:

listView.getListCell(index);
1
9
10/11/2018 7:27:18 AM

Accepted Answer

Unfortunately right now there is no API to get List Cell by index or to get All children's(listcells) for ListView. One solution can be, define a new StringProperty specialIndicator in your MyObject class.

 class MyObject {

        ....//u r properties
        private StringProperty specialIndicator;

When ever you get index from network signal set this specialIndicator property of object and do forcerefresh of ListView

public void onReceivedNetWorkSignalIndex() {
 listView.getItems().get(indexFromService).setSpecialIndicator("selected");
 listView.getItems().get(indexFromService+1).setSpecialIndicator("selectedplusone");
//force refresh listview (it will trigger cellFactory again so that you can manipulate layout)
 listView.setItems(null);
 listView.setItems(allObjects);

}

As you already have custom Object ListView , i am assuming you already have custom cellFactory (if not you have to create one ) ,Modify your custom cell factory to handle this special Indicators

         listView.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>() {
                    @Override
                    public ListCell<MyObject> call(ListView<MyObject> myObjectListView) {
                        ListCell<MyObject> cell = new ListCell<MyObject>(){
                            @Override
                            protected void updateItem(MyObject myObject, boolean b) {
                                super.updateItem(myObject, b);
                                if(myObject != null) {
                                    setText(myObject.getName());
                                    if("selected".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                        System.out.println("Setting new CSS/graphics for index retun from service." + myObject.getName());
                                    } else if("selectedplusone".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                        System.out.println("Setting new CSS/Graphics for index+1 returned from service" + myObject.getName());
                                    }
                              myObject.setSpecialIndicator(""); // reset it back to empty
                                }
                            }
                        };

                        return cell;
                    }
                });

Here is the whole sample Application ,you can look into it (in case the above explanation is not clear). enter image description here

public class ListViewTest extends Application {


    @Override
    public void start(Stage stage) throws Exception {

        VBox root = new VBox();
        final ObservableList<MyObject> allObjects = FXCollections.observableArrayList(new MyObject("object0"), new MyObject("object1"),new MyObject("object2"),new MyObject("object3"),new MyObject("object4"));
       final  ListView<MyObject> listView = new ListView<>(allObjects);
        listView.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>() {
            @Override
            public ListCell<MyObject> call(ListView<MyObject> myObjectListView) {
                ListCell<MyObject> cell = new ListCell<MyObject>(){
                    @Override
                    protected void updateItem(MyObject myObject, boolean b) {
                        super.updateItem(myObject, b);
                        if(myObject != null) {
                            setText(myObject.getName());
                            if("selected".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                System.out.println("Setting new CSS/graphics for index retun from service." + myObject.getName());
                                setText("I am selected Index from Service");
                            } else if("selectedplusone".equalsIgnoreCase(myObject.getSpecialIndicator())) {
                                System.out.println("Setting new CSS/Graphics for index+1 returned from service" + myObject.getName());
                                setText("I am selected Index +1  from Service");
                            }
                            myObject.setSpecialIndicator(""); // reset it back to empty
                        }
                    }
                };

                return cell;
            }
        });
        Button serviceIndex2 = new Button("ServiceIndex2");
        serviceIndex2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                int indexFromService =2;
                listView.getItems().get(indexFromService).setSpecialIndicator("selected");
                listView.getItems().get(indexFromService+1).setSpecialIndicator("selectedplusone");
                listView.setItems(null);
                listView.setItems(allObjects);
            }
        });
        root.getChildren().addAll(listView,serviceIndex2);
        Scene scene = new Scene(root,500,500);
        stage.setScene(scene);
        stage.show();

    }

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

    class MyObject {

        private StringProperty name;
        private StringProperty specialIndicator;

        MyObject(String name) {
            this.name = new SimpleStringProperty(name);
            this.specialIndicator = new SimpleStringProperty();
        }

        public String getName() {
            return name.get();
        }

        public StringProperty nameProperty() {
            return name;
        }

        public void setName(String name) {
            this.name.set(name);
        }

        public String getSpecialIndicator() {
            return specialIndicator.get();
        }

        public StringProperty specialIndicatorProperty() {
            return specialIndicator;
        }

        public void setSpecialIndicator(String specialIndicator) {
            this.specialIndicator.set(specialIndicator);
        }
    }
}
4
1/5/2014 9:56:14 PM

Cell has a style class called ".cell"

public Cell getListCell(ListView list, int index){
    Object[]cells = list.lookupAll(".cell").toArray();
    return (Cell)cells[index];
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon