JFoenix是否有一个CheckBoxListCell等效项,以便我们可以使用JFXCheckBox代替传统的?

时间:2018-06-27 18:57:22

标签: java javafx jfoenix

所以我目前正在使用我的JFXListView并尝试使用CheckBoxListCell在其中设置几个复选框。最初我是用这个的:

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

有没有办法让我可以使用JFXCheckBox代替传统的CheckBox?

1 个答案:

答案 0 :(得分:1)

您基本上只需要实现自己的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