CharmListView SelectedItemProperty? [英] CharmListView SelectedItemProperty?
问题描述
我正在使用 CharmListView
,只是注意到它没有ListView具有的 SelectionModel
。我以前用 listView.getSelectionModel()。SelectedItemProperty()。addListener()
用 ListView $ c来响应项目选择事件$ C>。怎么用
CharmListView?
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?
编辑
应用程序流程解释如下:
The app flow is explained below:
用户选择学校的一个部门。此列表位于 ListView
The user selects a department of a school. This list is in a ListView
然后是一个学期。这个其他列表位于 CharmListView
:
then a semester. This other list is in a CharmListView
:
SemesterPresenter
类的初始化
方法:
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());
});
}
第一次调用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.
推荐答案
现在,内部 ListView
控件的几个属性不会公开,例如selectionModel或focusModel。这些可能在收到的版本中。
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));
问题。
EDIT
根据OP提供的新信息,在 CharmListView
初始化之后,控件是已创建,但尚未创建其皮肤,因此此时子列表为空。
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.
添加 Platform.runLater() ;
只是延迟检索该列表控件完全创建皮肤所需的时间。
Adding Platform.runLater();
just delays the retrieval of that list the amount of time required for the control to fully create the skin.
这应该有效:
@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));
}
}
});
}
另外,根据皮肤创建的想法,这也可行,给你创建 ListView
的确切时刻:
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));
}
}
});
}
这篇关于CharmListView SelectedItemProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!