简体   繁体   English

JavaFX手风琴将进一步扩展面板

[英]JavaFX Accordion set next expanded Pane

In Accordion it is possible to set the selected Pane with accordion.setExpandedPane , and it is also possible to get all the children with accordion.getPanes() . 在Accordion中,可以通过accordion.setExpandedPane .setExpandedPane设置选定的Pane,还可以通过accordion.getPanes()获取所有子级。

However I'm struggling to find how to implement "select next Pane" functionality without explicitly extending the TitledPane class for the Panes, and maintaining an index manually via accordion.expandedPaneProperty() and some custom implementation. 但是,我正在努力寻找如何实现“选择下一个窗格”功能而又不显式扩展窗格的TitledPane类,以及如何通过accordion.expandedPaneProperty()和一些自定义实现手动维护索引的方法。

A Proposed solution would be to add a listened to expandedPaneProperty() : 一个建议的解决方案是添加一个监听的expandedPaneProperty()

    accordion.expandedPaneProperty().addListener((observable, oldValue, newValue) -> {
        if(null != newValue){
            idx = 0;
            for(TitledPane whytho: this.getPanes()){
                if(!whytho.equals(newValue))idx++;
                else break;
            }
            selectedIndex = idx;
        }
    });

Is there an easier way for this? 有更简单的方法吗?

You can query the index of the currently expanded pane using List#indexOf(Object) . 您可以使用List#indexOf(Object)查询当前展开的窗格的索引。 Then you just need to set the expanded pane to the pane at index ± 1 . 然后,您只需要将展开的窗格设置为index ± 1窗格即可。

private void expandPrevious(Accordion acc) {
    int index = acc.getPanes().indexOf(acc.getExpandedPane());
    int newIndex = Math.max(index - 1, 0);
    acc.setExpandedPane(acc.getPanes().get(newIndex));
}

private void expandNext(Accordion acc) {
    int index = acc.getPanes().indexOf(acc.getExpandedPane());
    int newIndex = Math.min(index + 1, acc.getPanes().size() - 1);
    acc.setExpandedPane(acc.getPanes().get(newIndex));
}

The above doesn't perform any wrap around logic; 上面没有执行任何环绕逻辑; in other words, calling expandNext while the last pane is expanded won't expand the first pane—rather nothing will change. 换句话说,在扩展最后一个窗格时调用expandNext不会扩展第一个窗格,而是什么也不会改变。 It also doesn't handle the case where getExpandedPane() returns null . 它也不能处理getExpandedPane()返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM