我尝试使用JavaFX 8实现一个应用程序,其中我有一个GridPanes的ListView,其中每个GridPane由2个其他控件(ChoiceBoxes)组成。 ListView中的Gridpanes由用户在运行时动态添加。我喜欢的是为ChoiceBox添加一个监听器,以便我知道如果用户做出选择,所选择的选择框属于哪个ListView项/索引。
我的问题是,当用户选择选项框的项目时,我无法获取ListView的索引,因为在这种情况下,ListView中的项目(即Gridpane)未被(自动)选择或聚焦。
anybode可以帮助我吗?
答案 0 :(得分:0)
感谢您的评论,我现在通过查找选择框的父级来获得解决方案(请注意,paneList是一个ArrayList,我在其中存储了GridPanes的引用,其中由用户添加)
// define Listener for choiceBox
choiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
GridPane parent = (GridPane) choiceBox.getParent();
// keep track of selected choice
selectedChoice = newValue;
logger.debug("selected choice: {}", selectedChoice);
// keep also track of parent GridPane
selectedGridpane = paneList.indexOf(parent);
logger.debug("selected GridPane {}", paneList.indexOf(parent));
});