JavaFX-在ListView中创建不可选择的项目

时间:2018-07-20 03:44:43

标签: java javafx

我正在使用jfoenix库中的JFXListViewJFXListCell,其目的和功能与常规ListView相同。

该列表包含一些LabelButtonAnchorPane。在列表的顶部和底部,我想添加不可选择的项目。项目should not be selectable on mouse clickshould not be able to focusshould not be able to scroll

尽管我使用updateItem()功能并设置了禁用项:

@FXML
JFXListView listView;
ObservableList<AnchorPane> list = FXCollections.observableArrayList();

private void initializeListView(){

    AnchorPane headerBottomPane = new AnchorPane();
    headerBottomPane.setId("headerBottomPane");
    ....//some property of AnchorPane
    list.add(headerBottomPane); //Add header AnchorPane

    while(true){
        AnchorPane listContainer = new AnchorPane();
        Label title = new Label();
        Label subtitle = new Label();
        Button button = new Button();
        Button button2 = new Button();
        //Some code here...

        listContainer.getChildren().addAll(label, subtitle, button, button2);

        list.add(listContainer);
        //some code here...
    }

    list.add(headerBottomPane); //Add bottom AnchorPane

    listView.setCellFactory(new CallBack<JFXListView, JFXListCell>(){
        @Override
        public JFXListCell call(JFXListView param){
            return new JFXListCell<AnchorPane>(){
                @Override
                protected void updateItem(AnchorPane anchorPane, boolean empty){
                    super.updateItem(anchorPane, empty);
                    if(anchorPane != null){
                        if(anchorPane.getId.equals("headerBottomPane")){
                            setDisable(true);
                        }
                        setItem(anchorPane);
                    }else{
                        setItem(null);
                    }
                }
            };
        }
    });
}

我可以禁用列表的第一项和最后一项,该项目不再能够使用mouseClick进行选择。

但是问题是,当我使用Keyboard arrow uparrow down时,它是可以聚焦的。另一个奇怪的事情是,当我使用mouse wheel来滚动列表时,某些项目正在变得也是不可选择的。

2 个答案:

答案 0 :(得分:1)

我认为您必须将listView.setCellFactory()函数放在添加这些项目的代码顶部,在添加该项目之前尝试对其进行初始化。

在您的updateItem()内尝试使用setMouseTransparent()setFocusTravesable()

            @Override
            protected void updateItem(AnchorPane anchorPane, boolean empty){
                super.updateItem(anchorPane, empty);
                if(anchorPane != null){
                    if(anchorPane.getId.equals("headerBottomPane")){
                        setItem(anchorPane); //moved at the top
                        setMouseTransparent(true); //added this line
                        setFocusTraversable(false); //added this line
                        setDisable(true);
                }else{
                    setItem(null);
                }
            }

我还没有测试过,但我希望它能工作。

答案 1 :(得分:0)

我会想到只使用一个VBox,然后将顶部的不可选择项放在第一位,然后将ListView与所有可选择的项放在一起,再将底部的不可选择项放在……...

相关问题