add Unique JavaFX Tab - Java JavaFX

Java examples for JavaFX:TabPane

Description

add Unique JavaFX Tab

Demo Code

/**/*from www.  j a  v a 2 s .  co  m*/
 * Author: obullxl@gmail.com
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
//package com.java2s;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

public class Main {

    public static void addUniqueTab(TabPane tabpane, Tab uniqueTab) {
        if (tabpane == null) {
            return;
        }

        Object userData = findTabUserData(uniqueTab);
        if (userData == null) {
            return;
        }

        Tab existTab = findTab(tabpane, userData);
        if (existTab != null) {
            // ????
            tabpane.getTabs().removeAll(existTab);
        }

        // ???????
        addAndActiveTab(tabpane, uniqueTab);
    }

    @SuppressWarnings("unchecked")
    public static <T> T findTabUserData(Tab tab) {
        if (tab == null) {
            return null;
        }

        Object object = tab.getUserData();
        if (object == null) {
            return null;
        }

        return (T) object;
    }

    public static Tab findTab(TabPane tabpane, Object userData) {
        if (userData == null) {
            return null;
        }

        if (tabpane == null) {
            return null;
        }

        for (Tab tab : tabpane.getTabs()) {
            Object tabUserData = findTabUserData(tab);
            if (tabUserData == null) {
                return null;
            }

            if (userData.equals(tabUserData)) {
                // ??
                return tab;
            }
        }

        // ????
        return null;
    }

    public static void addAndActiveTab(TabPane tabpane, Tab tab) {
        if (tab == null) {
            return;
        }

        if (tabpane == null) {
            return;
        }

        tabpane.getTabs().add(tab);
        tabpane.getSelectionModel().select(tab);
    }
}

Related Tutorials