Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

javafx - CharmListView SelectedItemProperty?

I am using the CharmListView and just noticed that it doesn't have a SelectionModel that the ListView has. I used to use listView.getSelectionModel().SelectedItemProperty().addListener() to respond to an item selection event with a ListView. How is that done with the CharmListView?

EDIT

The app flow is explained below:

The user selects a department of a school. This list is in a ListView

enter image description here

then a semester. This other list is in a CharmListView:

enter image description here

The initialize method of the SemesterPresenter class:

public void initialize(URL url, ResourceBundle rb) {
    loadSemesters();
    semesterListView.setItems(semesters);
    semesterListView.setHeadersFunction(Level::getLevel);
    MobileApplication.getInstance().getView().showingProperty().addListener((obs,ov,nv)->{
        System.out.println(semesterListView.getChildrenUnmodifiable());
    });
}

The first call to getChildrenUnmodifiable() returns an empty array. It's the same scenario when using all the propositions in the posts below with null pointers returned.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));
            }
        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...