CharmListView SelectedItemProperty?

前端 未结 1 1929
眼角桃花
眼角桃花 2021-01-23 11:47

I am using the CharmListView and just noticed that it doesn\'t have a SelectionModel that the ListView has. I used to use listView.getSelectionMo

相关标签:
1条回答
  • 2021-01-23 12:26

    For now several properties of the inner ListView control are not exposed, like the selectionModel or the focusModel. Those may be in incoming releases.

    For now, as a workaround you can lookup for it:

    CharmListView<?, ?> charmListView = new CharmListView<>();
    ...
    stage.show();
    ...
    ListView innerList = (ListView)charmListView.lookup(".list-view");
    innerList.getSelectionModel().selectedItemProperty().addListener(
        (obs, ov, nv) -> System.out.println("Selected: " + nv));
    

    An issue has been filed with this request.

    EDIT

    Based on the new info provided by the OP, right after the CharmListView is initialized, the control is created, but it hasn't created its skin yet, so the list of children is empty at this moment.

    Adding Platform.runLater(); just delays the retrieval of that list the amount of time required for the control to fully create the skin.

    This should work:

    @FXML
    private CharmListView<String, String> charmListView;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        charmListView.setItems(FXCollections.observableArrayList("This", "is", "a", "test"));
    
        Platform.runLater(() -> {
            for (Node node : charmListView.getChildrenUnmodifiable()) {
                if (node instanceof ListView) {
                    ((ListView)node).getSelectionModel().selectedItemProperty().addListener(
                            (obs, ov, nv) -> System.out.println(nv));
                }
            }
        });
    }
    

    Also, based on the idea of the skin creation, this will work as well, giving you the exact moment when the ListView is created:

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        charmListView.setItems(FXCollections.observableArrayList("This", "is", "a", "test"));
        charmListView.skinProperty().addListener((obs, ov, nv) -> {
            for (Node node : charmListView.getChildrenUnmodifiable()) {
                if (node instanceof ListView) {
                    ((ListView)node).getSelectionModel().selectedItemProperty().addListener(
                            (obs2, ov2, nv2) -> System.out.println(nv2));
                }
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题