Search code examples
javajavafxjfoenix

Is there a CheckBoxListCell equivalent for JFoenix so that we can use JFXCheckBox instead of the traditional one?


So I am currently using my JFXListView and trying to set several checkboxes inside of it using CheckBoxListCell. Originally I used this:

listView.setCellFactory(CheckBoxListCell.forListView(new Callback<classForMenuOptions, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(UserMenuOptions item) {
                return item.selectedProperty();
            }
}));

Is there a way so that I can use JFXCheckBox instead of the traditional CheckBox?


Solution

  • You basically just need to implement your own cellFactory.

    import com.jfoenix.controls.JFXCheckBox;
    import com.jfoenix.controls.JFXListView;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.ListCell;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class ListViewExperiments extends Application
    {
    
        @Override
        public void start(Stage primaryStage) throws Exception
        {
            primaryStage.setTitle("ListView Experiment 1");
    
            JFXListView<String> listView = new JFXListView<>();
            listView.setPrefWidth(200);
            listView.setCellFactory(lv -> new ListCell<String>()
            {
                JFXCheckBox checkBox = new JFXCheckBox();
    
                @Override
                public void updateItem(String item, boolean empty)
                {
                    super.updateItem(item, empty);
                    if (empty) {
                        //setText(null);
                        setGraphic(null);
                    }
                    else {
                        checkBox.setText(item);
                        setGraphic(checkBox);
                    }
                }
            });
            listView.getItems().add("Item 1");
            listView.getItems().add("Item 2");
            listView.getItems().add("Item 3");
    
            HBox hbox = new HBox(listView);
    
            Scene scene = new Scene(hbox, 300, 120);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args)
        {
            Application.launch(args);
        }
    }
    

    enter image description here