如何通过javafx中TabPane中选项卡的属性文本获取选项卡?

时间:2018-04-10 12:21:18

标签: javafx javafx-8 javafx-2

我想通过标签属性text获取标签,但我找不到原生方法。所以我写了一个课来实现这个目的。 题: - 我想知道是否有为此目的的原生方法? - 有更好的实施吗?

谢谢!

```

    package pre.huangjs.tabpane;

    import javafx.collections.ListChangeListener;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;

    import java.util.HashMap;
    import java.util.List;

    /**
     * Created by huangjs on 2018/4/9.
     */
    public class TabPaneExpansion {
        private TabPane tabPane;
        private HashMap<String, Tab> tabsMap;

        public TabPane getTabPane() {
            return tabPane;
        }

        public void setTabPane(TabPane tabPane) {
            this.tabPane = tabPane;
        }

        public TabPaneExpansion() {
            this.tabPane = new TabPane();
            this.tabsMap = new HashMap<>();
            initial();
        }

        public TabPaneExpansion(TabPane tabPane) {
            this.tabPane = tabPane;
            this.tabsMap = new HashMap<>();
            initial();
        }

        private void initial() {
            tabPane.getTabs().addListener(new ListChangeListener<Tab>() {

                @Override
                public void onChanged(Change<? extends Tab> c) {
                    while (c.next()) {

                        // if elements were added into list, the elements's text
                        // and the elements themselves need to be added into HashMap
                        if (c.wasAdded()) {
                            List<? extends Tab> addedTabs = c.getAddedSubList();
                            for (Tab tab : addedTabs) {
                                tabsMap.put(tab.getText(), tab);
                            }
                        }

                        // if elements were removed from list, the elements's text
                        // and the elements themselves need to be removed from HashMap
                        if(c.wasRemoved()){
                            List<? extends Tab> removedTabs = c.getRemoved();
                            for(Tab tab : removedTabs){
                                tabsMap.remove(tab.getText());
                            }
                        }
                    }

                }
            });
        }

        public boolean addTab(Tab tab) {
            return this.tabPane.getTabs().add(tab);
        }

        public boolean addTabs(Tab... tabs) {
           return this.tabPane.getTabs().addAll(tabs);
        }

        public boolean removeTab(String text){
            return this.tabPane.getTabs().remove(getTabByText(text));
        }



  public Tab getTabByText(String text) {
        return tabsMap.get(text);
    }
}

```

0 个答案:

没有答案