Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ListCell via ListView

Tags:

javafx

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);
like image 840
Frank Roth Avatar asked Jan 05 '14 16:01

Frank Roth


2 Answers

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);
        }
    }
}
like image 148
invariant Avatar answered Sep 22 '22 08:09

invariant


Here's a relatively simple approach, where there is just one "selected" index. Here I create a property to hold the index that is selected, and the cell factory just observes it, along with the cell's item property and index property, and sets the text via a binding. You could do something similar to set the graphic, if needed.

import java.util.concurrent.Callable;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ListViewStyleAroundSelection extends Application {

    @Override
    public void start(Stage primaryStage) {
        final ListView<String> listView = new ListView<>();
        for (int i=1; i<=20; i++) {
            listView.getItems().add("Item "+i);
        }
        final HBox controls = new HBox(5);
        final Button button = new Button("Set selection");
        final TextField indexField = new TextField();

        final IntegerProperty selectionIndex = new SimpleIntegerProperty();
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                try {
                    selectionIndex.set(Integer.parseInt(indexField.getText()));
                } catch (NumberFormatException nfe) {
                    indexField.setText("");
                }
            }

        });

        controls.getChildren().addAll(new Label("Enter selection index:"), indexField, button);
        final BorderPane root = new BorderPane();
        root.setCenter(listView);
        root.setBottom(controls);

        listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {

            @Override
            public ListCell<String> call(ListView<String> lv) {
                final ListCell<String> cell = new ListCell<>();
                cell.textProperty().bind(Bindings.createStringBinding(new Callable<String>() {

                    @Override
                    public String call() throws Exception {
                        if (cell.getItem() == null) {
                            return null ;
                        } else {
                            switch (cell.getIndex() - selectionIndex.get()) {
                              case -1: return cell.getItem() + " (selected item below)"; 
                              case 0: return cell.getItem() + " (selected)";
                              case 1: return cell.getItem() + " (selected item above)";
                              default: return cell.getItem();
                            }
                        }
                    }

                }, cell.itemProperty(), cell.indexProperty(), selectionIndex));
                return cell;
            }

        });

        final Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

And here's a slightly more complex version. Here I have a custom data type which includes a boolean property. The update sets the boolean property of the specified item to true. The cell factory creates a cell, and observes the selected property both of the current item and of the previous item. Then, as before, it uses a binding to update the text of the cell.

import java.util.concurrent.Callable;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.IntegerBinding;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ListViewStyleAroundSelection extends Application {

    @Override
    public void start(Stage primaryStage) {
        final ListView<MyDataType> listView = new ListView<>();
        for (int i=0; i<=20; i++) {
            listView.getItems().add(new MyDataType("Item "+i, false));
        }
        final HBox controls = new HBox(5);
        controls.setPadding(new Insets(5));
        final Button button = new Button("Set selection");
        final TextField indexField = new TextField();

        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                try {
                    int index = Integer.parseInt(indexField.getText());
                    if (index >= 0 && index < listView.getItems().size()) {
                        final MyDataType item = listView.getItems().get(index);
                        item.setSelected( ! item.isSelected() );
                    }
                } catch (NumberFormatException nfe) {
                    indexField.setText("");
                }
            }

        });

        controls.getChildren().addAll(new Label("Enter selection index:"), indexField, button);
        final BorderPane root = new BorderPane();
        root.setCenter(listView);
        root.setBottom(controls);

        listView.setCellFactory(new Callback<ListView<MyDataType>, ListCell<MyDataType>>() {

            @Override
            public ListCell<MyDataType> call(ListView<MyDataType> lv) {
                final ListCell<MyDataType> cell = new ListCell<>();
                final IntegerBinding previousIndex = cell.indexProperty().subtract(1);
                final ObjectBinding<MyDataType> previousItem = Bindings.valueAt(listView.getItems(), previousIndex);
                final BooleanBinding previousItemSelected = Bindings.selectBoolean(previousItem, "selected");
                final StringBinding thisItemName = Bindings.selectString(cell.itemProperty(), "name");
                final BooleanBinding thisItemSelected = Bindings.selectBoolean(cell.itemProperty(), "selected");
                cell.textProperty().bind(Bindings.createStringBinding(new Callable<String>() {

                    @Override
                    public String call() throws Exception {
                        if (cell.getItem() == null) {
                            return null ;
                        } else {
                            String value = cell.getItem().getName();
                            if (thisItemSelected.get()) {
                                value = value + " (selected) " ;
                            } else if (previousItemSelected.get()) {
                                value = value + " (selected item is above)";
                            }
                            return value ;
                        }
                    }

                }, thisItemName, thisItemSelected, previousItemSelected));
                return cell;
            }

        });

        final Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static class MyDataType {
        private final BooleanProperty selected ;
        private final StringProperty name ;
        public MyDataType(String name, boolean selected) {
            this.name = new SimpleStringProperty(this, "name", name);
            this.selected = new SimpleBooleanProperty(this, "selected", selected);
        }
        public final String getName() {
            return name.get();
        }
        public final void setName(String name) {
            this.name.set(name);
        }
        public final StringProperty nameProperty() {
            return name ;
        }
        public final boolean isSelected() {
            return selected.get();
        }
        public final void setSelected(boolean selected) {
            this.selected.set(selected);
        }
        public final BooleanProperty selectedProperty() {
            return selected;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
like image 32
James_D Avatar answered Sep 25 '22 08:09

James_D