Java Code Examples for sun.swing.SwingUtilities2#tabbedPaneChangeFocusTo()

The following examples show how to use sun.swing.SwingUtilities2#tabbedPaneChangeFocusTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicTabbedPaneUI.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
boolean requestFocusForVisibleComponent() {
    return SwingUtilities2.tabbedPaneChangeFocusTo(getVisibleComponent());
}
 
Example 2
Source File: JTabbedPane.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Removes the tab at <code>index</code>.
 * After the component associated with <code>index</code> is removed,
 * its visibility is reset to true to ensure it will be visible
 * if added to other containers.
 * @param index the index of the tab to be removed
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #addTab
 * @see #insertTab
 */
public void removeTabAt(int index) {
    checkIndex(index);

    Component component = getComponentAt(index);
    boolean shouldChangeFocus = false;
    int selected = getSelectedIndex();
    String oldName = null;

    /* if we're about to remove the visible component */
    if (component == visComp) {
        shouldChangeFocus = (SwingUtilities.findFocusOwner(visComp) != null);
        visComp = null;
    }

    if (accessibleContext != null) {
        /* if we're removing the selected page */
        if (index == selected) {
            /* fire an accessible notification that it's unselected */
            pages.get(index).firePropertyChange(
                AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
                AccessibleState.SELECTED, null);

            oldName = accessibleContext.getAccessibleName();
        }

        accessibleContext.firePropertyChange(
                AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
                component, null);
    }

    // Force the tabComponent to be cleaned up.
    setTabComponentAt(index, null);
    pages.remove(index);

    // NOTE 4/15/2002 (joutwate):
    // This fix is implemented using client properties since there is
    // currently no IndexPropertyChangeEvent.  Once
    // IndexPropertyChangeEvents have been added this code should be
    // modified to use it.
    putClientProperty("__index_to_remove__", Integer.valueOf(index));

    /* if the selected tab is after the removal */
    if (selected > index) {
        setSelectedIndexImpl(selected - 1, false);

    /* if the selected tab is the last tab */
    } else if (selected >= getTabCount()) {
        setSelectedIndexImpl(selected - 1, false);
        Page newSelected = (selected != 0)
            ? pages.get(selected - 1)
            : null;

        changeAccessibleSelection(null, oldName, newSelected);

    /* selected index hasn't changed, but the associated tab has */
    } else if (index == selected) {
        fireStateChanged();
        changeAccessibleSelection(null, oldName, pages.get(index));
    }

    // We can't assume the tab indices correspond to the
    // container's children array indices, so make sure we
    // remove the correct child!
    if (component != null) {
        Component components[] = getComponents();
        for (int i = components.length; --i >= 0; ) {
            if (components[i] == component) {
                super.remove(i);
                component.setVisible(true);
                break;
            }
        }
    }

    if (shouldChangeFocus) {
        SwingUtilities2.tabbedPaneChangeFocusTo(getSelectedComponent());
    }

    revalidate();
    repaint();
}
 
Example 3
Source File: JTabbedPane.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Removes the tab at <code>index</code>.
 * After the component associated with <code>index</code> is removed,
 * its visibility is reset to true to ensure it will be visible
 * if added to other containers.
 * @param index the index of the tab to be removed
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #addTab
 * @see #insertTab
 */
public void removeTabAt(int index) {
    checkIndex(index);

    Component component = getComponentAt(index);
    boolean shouldChangeFocus = false;
    int selected = getSelectedIndex();
    String oldName = null;

    /* if we're about to remove the visible component */
    if (component == visComp) {
        shouldChangeFocus = (SwingUtilities.findFocusOwner(visComp) != null);
        visComp = null;
    }

    if (accessibleContext != null) {
        /* if we're removing the selected page */
        if (index == selected) {
            /* fire an accessible notification that it's unselected */
            pages.get(index).firePropertyChange(
                AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
                AccessibleState.SELECTED, null);

            oldName = accessibleContext.getAccessibleName();
        }

        accessibleContext.firePropertyChange(
                AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
                component, null);
    }

    // Force the tabComponent to be cleaned up.
    setTabComponentAt(index, null);
    pages.remove(index);

    // NOTE 4/15/2002 (joutwate):
    // This fix is implemented using client properties since there is
    // currently no IndexPropertyChangeEvent.  Once
    // IndexPropertyChangeEvents have been added this code should be
    // modified to use it.
    putClientProperty("__index_to_remove__", Integer.valueOf(index));

    /* if the selected tab is after the removal */
    if (selected > index) {
        setSelectedIndexImpl(selected - 1, false);

    /* if the selected tab is the last tab */
    } else if (selected >= getTabCount()) {
        setSelectedIndexImpl(selected - 1, false);
        Page newSelected = (selected != 0)
            ? pages.get(selected - 1)
            : null;

        changeAccessibleSelection(null, oldName, newSelected);

    /* selected index hasn't changed, but the associated tab has */
    } else if (index == selected) {
        fireStateChanged();
        changeAccessibleSelection(null, oldName, pages.get(index));
    }

    // We can't assume the tab indices correspond to the
    // container's children array indices, so make sure we
    // remove the correct child!
    if (component != null) {
        Component components[] = getComponents();
        for (int i = components.length; --i >= 0; ) {
            if (components[i] == component) {
                super.remove(i);
                component.setVisible(true);
                break;
            }
        }
    }

    if (shouldChangeFocus) {
        SwingUtilities2.tabbedPaneChangeFocusTo(getSelectedComponent());
    }

    revalidate();
    repaint();
}
 
Example 4
Source File: JTabbedPane.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 5
Source File: JTabbedPane.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Removes the tab at <code>index</code>.
 * After the component associated with <code>index</code> is removed,
 * its visibility is reset to true to ensure it will be visible
 * if added to other containers.
 * @param index the index of the tab to be removed
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #addTab
 * @see #insertTab
 */
public void removeTabAt(int index) {
    checkIndex(index);

    Component component = getComponentAt(index);
    boolean shouldChangeFocus = false;
    int selected = getSelectedIndex();
    String oldName = null;

    /* if we're about to remove the visible component */
    if (component == visComp) {
        shouldChangeFocus = (SwingUtilities.findFocusOwner(visComp) != null);
        visComp = null;
    }

    if (accessibleContext != null) {
        /* if we're removing the selected page */
        if (index == selected) {
            /* fire an accessible notification that it's unselected */
            pages.get(index).firePropertyChange(
                AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
                AccessibleState.SELECTED, null);

            oldName = accessibleContext.getAccessibleName();
        }

        accessibleContext.firePropertyChange(
                AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
                component, null);
    }

    // Force the tabComponent to be cleaned up.
    setTabComponentAt(index, null);
    pages.remove(index);

    // NOTE 4/15/2002 (joutwate):
    // This fix is implemented using client properties since there is
    // currently no IndexPropertyChangeEvent.  Once
    // IndexPropertyChangeEvents have been added this code should be
    // modified to use it.
    putClientProperty("__index_to_remove__", Integer.valueOf(index));

    /* if the selected tab is after the removal */
    if (selected > index) {
        setSelectedIndexImpl(selected - 1, false);

    /* if the selected tab is the last tab */
    } else if (selected >= getTabCount()) {
        setSelectedIndexImpl(selected - 1, false);
        Page newSelected = (selected != 0)
            ? pages.get(selected - 1)
            : null;

        changeAccessibleSelection(null, oldName, newSelected);

    /* selected index hasn't changed, but the associated tab has */
    } else if (index == selected) {
        fireStateChanged();
        changeAccessibleSelection(null, oldName, pages.get(index));
    }

    // We can't assume the tab indices correspond to the
    // container's children array indices, so make sure we
    // remove the correct child!
    if (component != null) {
        Component components[] = getComponents();
        for (int i = components.length; --i >= 0; ) {
            if (components[i] == component) {
                super.remove(i);
                component.setVisible(true);
                break;
            }
        }
    }

    if (shouldChangeFocus) {
        SwingUtilities2.tabbedPaneChangeFocusTo(getSelectedComponent());
    }

    revalidate();
    repaint();
}
 
Example 6
Source File: JTabbedPane.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 7
Source File: JTabbedPane.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the component at <code>index</code> to <code>component</code>.
 * An internal exception is raised if there is no tab at that index.
 *
 * @param index the tab index where this component is being placed
 * @param component the component for the tab
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #getComponentAt
 * @beaninfo
 *    attribute: visualUpdate true
 *  description: The component at the specified tab index.
 */
public void setComponentAt(int index, Component component) {
    Page page = pages.get(index);
    if (component != page.component) {
        boolean shouldChangeFocus = false;

        if (page.component != null) {
            shouldChangeFocus =
                (SwingUtilities.findFocusOwner(page.component) != null);

            // REMIND(aim): this is really silly;
            // why not if (page.component.getParent() == this) remove(component)
            synchronized(getTreeLock()) {
                int count = getComponentCount();
                Component children[] = getComponents();
                for (int i = 0; i < count; i++) {
                    if (children[i] == page.component) {
                        super.remove(i);
                    }
                }
            }
        }

        page.component = component;
        boolean selectedPage = (getSelectedIndex() == index);

        if (selectedPage) {
            this.visComp = component;
        }

        if (component != null) {
            component.setVisible(selectedPage);
            addImpl(component, null, -1);

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(component);
            }
        } else {
            repaint();
        }

        revalidate();
    }
}
 
Example 8
Source File: JTabbedPane.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the component at <code>index</code> to <code>component</code>.
 * An internal exception is raised if there is no tab at that index.
 *
 * @param index the tab index where this component is being placed
 * @param component the component for the tab
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #getComponentAt
 */
@BeanProperty(visualUpdate = true, description
        = "The component at the specified tab index.")
@SuppressWarnings("deprecation")
public void setComponentAt(int index, Component component) {
    Page page = pages.get(index);
    if (component != page.component) {
        boolean shouldChangeFocus = false;

        if (page.component != null) {
            shouldChangeFocus =
                (SwingUtilities.findFocusOwner(page.component) != null);

            // REMIND(aim): this is really silly;
            // why not if (page.component.getParent() == this) remove(component)
            synchronized(getTreeLock()) {
                int count = getComponentCount();
                Component children[] = getComponents();
                for (int i = 0; i < count; i++) {
                    if (children[i] == page.component) {
                        super.remove(i);
                    }
                }
            }
        }

        page.component = component;
        boolean selectedPage = (getSelectedIndex() == index);

        if (selectedPage) {
            this.visComp = component;
        }

        if (component != null) {
            component.setVisible(selectedPage);
            addImpl(component, null, -1);

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(component);
            }
        } else {
            repaint();
        }

        revalidate();
    }
}
 
Example 9
Source File: JTabbedPane.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 10
Source File: JTabbedPane.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the component at <code>index</code> to <code>component</code>.
 * An internal exception is raised if there is no tab at that index.
 *
 * @param index the tab index where this component is being placed
 * @param component the component for the tab
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #getComponentAt
 * @beaninfo
 *    attribute: visualUpdate true
 *  description: The component at the specified tab index.
 */
public void setComponentAt(int index, Component component) {
    Page page = pages.get(index);
    if (component != page.component) {
        boolean shouldChangeFocus = false;

        if (page.component != null) {
            shouldChangeFocus =
                (SwingUtilities.findFocusOwner(page.component) != null);

            // REMIND(aim): this is really silly;
            // why not if (page.component.getParent() == this) remove(component)
            synchronized(getTreeLock()) {
                int count = getComponentCount();
                Component children[] = getComponents();
                for (int i = 0; i < count; i++) {
                    if (children[i] == page.component) {
                        super.remove(i);
                    }
                }
            }
        }

        page.component = component;
        boolean selectedPage = (getSelectedIndex() == index);

        if (selectedPage) {
            this.visComp = component;
        }

        if (component != null) {
            component.setVisible(selectedPage);
            addImpl(component, null, -1);

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(component);
            }
        } else {
            repaint();
        }

        revalidate();
    }
}
 
Example 11
Source File: JTabbedPane.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 12
Source File: JTabbedPane.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 13
Source File: BasicTabbedPaneUI.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
boolean requestFocusForVisibleComponent() {
    return SwingUtilities2.tabbedPaneChangeFocusTo(getVisibleComponent());
}
 
Example 14
Source File: BasicTabbedPaneUI.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
boolean requestFocusForVisibleComponent() {
    return SwingUtilities2.tabbedPaneChangeFocusTo(getVisibleComponent());
}
 
Example 15
Source File: JTabbedPane.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the component at <code>index</code> to <code>component</code>.
 * An internal exception is raised if there is no tab at that index.
 *
 * @param index the tab index where this component is being placed
 * @param component the component for the tab
 * @exception IndexOutOfBoundsException if index is out of range
 *            {@code (index < 0 || index >= tab count)}
 *
 * @see #getComponentAt
 * @beaninfo
 *    attribute: visualUpdate true
 *  description: The component at the specified tab index.
 */
public void setComponentAt(int index, Component component) {
    Page page = pages.get(index);
    if (component != page.component) {
        boolean shouldChangeFocus = false;

        if (page.component != null) {
            shouldChangeFocus =
                (SwingUtilities.findFocusOwner(page.component) != null);

            // REMIND(aim): this is really silly;
            // why not if (page.component.getParent() == this) remove(component)
            synchronized(getTreeLock()) {
                int count = getComponentCount();
                Component children[] = getComponents();
                for (int i = 0; i < count; i++) {
                    if (children[i] == page.component) {
                        super.remove(i);
                    }
                }
            }
        }

        page.component = component;
        boolean selectedPage = (getSelectedIndex() == index);

        if (selectedPage) {
            this.visComp = component;
        }

        if (component != null) {
            component.setVisible(selectedPage);
            addImpl(component, null, -1);

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(component);
            }
        } else {
            repaint();
        }

        revalidate();
    }
}
 
Example 16
Source File: JTabbedPane.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 17
Source File: JTabbedPane.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}
 
Example 18
Source File: BasicTabbedPaneUI.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
boolean requestFocusForVisibleComponent() {
    return SwingUtilities2.tabbedPaneChangeFocusTo(getVisibleComponent());
}
 
Example 19
Source File: BasicTabbedPaneUI.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
boolean requestFocusForVisibleComponent() {
    return SwingUtilities2.tabbedPaneChangeFocusTo(getVisibleComponent());
}
 
Example 20
Source File: JTabbedPane.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 * to each registered listener. This method is called each time there is
 * a change to either the selected index or the selected tab in the
 * {@code JTabbedPane}. Usually, the selected index and selected tab change
 * together. However, there are some cases, such as tab addition, where the
 * selected index changes and the same tab remains selected. There are other
 * cases, such as deleting the selected tab, where the index remains the
 * same, but a new tab moves to that index. Events are fired for all of
 * these cases.
 *
 * @see #addChangeListener
 * @see EventListenerList
 */
protected void fireStateChanged() {
    /* --- Begin code to deal with visibility --- */

    /* This code deals with changing the visibility of components to
     * hide and show the contents for the selected tab. It duplicates
     * logic already present in BasicTabbedPaneUI, logic that is
     * processed during the layout pass. This code exists to allow
     * developers to do things that are quite difficult to accomplish
     * with the previous model of waiting for the layout pass to process
     * visibility changes; such as requesting focus on the new visible
     * component.
     *
     * For the average code, using the typical JTabbedPane methods,
     * all visibility changes will now be processed here. However,
     * the code in BasicTabbedPaneUI still exists, for the purposes
     * of backward compatibility. Therefore, when making changes to
     * this code, ensure that the BasicTabbedPaneUI code is kept in
     * synch.
     */

    int selIndex = getSelectedIndex();

    /* if the selection is now nothing */
    if (selIndex < 0) {
        /* if there was a previous visible component */
        if (visComp != null && visComp.isVisible()) {
            /* make it invisible */
            visComp.setVisible(false);
        }

        /* now there's no visible component */
        visComp = null;

    /* else - the selection is now something */
    } else {
        /* Fetch the component for the new selection */
        Component newComp = getComponentAt(selIndex);

        /* if the new component is non-null and different */
        if (newComp != null && newComp != visComp) {
            boolean shouldChangeFocus = false;

            /* Note: the following (clearing of the old visible component)
             * is inside this if-statement for good reason: Tabbed pane
             * should continue to show the previously visible component
             * if there is no component for the chosen tab.
             */

            /* if there was a previous visible component */
            if (visComp != null) {
                shouldChangeFocus =
                    (SwingUtilities.findFocusOwner(visComp) != null);

                /* if it's still visible */
                if (visComp.isVisible()) {
                    /* make it invisible */
                    visComp.setVisible(false);
                }
            }

            if (!newComp.isVisible()) {
                newComp.setVisible(true);
            }

            if (shouldChangeFocus) {
                SwingUtilities2.tabbedPaneChangeFocusTo(newComp);
            }

            visComp = newComp;
        } /* else - the visible component shouldn't changed */
    }

    /* --- End code to deal with visibility --- */

    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ChangeListener.class) {
            // Lazily create the event:
            if (changeEvent == null)
                changeEvent = new ChangeEvent(this);
            ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
        }
    }
}