< prev index next >

src/java.desktop/share/classes/javax/swing/JTabbedPane.java

Print this page




 316      * @since 1.4
 317      */
 318     public ChangeListener[] getChangeListeners() {
 319         return listenerList.getListeners(ChangeListener.class);
 320     }
 321 
 322     /**
 323      * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 324      * to each registered listener. This method is called each time there is
 325      * a change to either the selected index or the selected tab in the
 326      * {@code JTabbedPane}. Usually, the selected index and selected tab change
 327      * together. However, there are some cases, such as tab addition, where the
 328      * selected index changes and the same tab remains selected. There are other
 329      * cases, such as deleting the selected tab, where the index remains the
 330      * same, but a new tab moves to that index. Events are fired for all of
 331      * these cases.
 332      *
 333      * @see #addChangeListener
 334      * @see EventListenerList
 335      */

 336     protected void fireStateChanged() {
 337         /* --- Begin code to deal with visibility --- */
 338 
 339         /* This code deals with changing the visibility of components to
 340          * hide and show the contents for the selected tab. It duplicates
 341          * logic already present in BasicTabbedPaneUI, logic that is
 342          * processed during the layout pass. This code exists to allow
 343          * developers to do things that are quite difficult to accomplish
 344          * with the previous model of waiting for the layout pass to process
 345          * visibility changes; such as requesting focus on the new visible
 346          * component.
 347          *
 348          * For the average code, using the typical JTabbedPane methods,
 349          * all visibility changes will now be processed here. However,
 350          * the code in BasicTabbedPaneUI still exists, for the purposes
 351          * of backward compatibility. Therefore, when making changes to
 352          * this code, ensure that the BasicTabbedPaneUI code is kept in
 353          * synch.
 354          */
 355 


 932             // Container.add() interprets -1 as "append", so convert
 933             // the index appropriately to be handled by the vector
 934             insertTab(title, icon, component, null, index == -1? getTabCount() : index);
 935         } else {
 936             super.add(component, constraints, index);
 937         }
 938     }
 939 
 940     /**
 941      * Removes the tab at <code>index</code>.
 942      * After the component associated with <code>index</code> is removed,
 943      * its visibility is reset to true to ensure it will be visible
 944      * if added to other containers.
 945      * @param index the index of the tab to be removed
 946      * @exception IndexOutOfBoundsException if index is out of range
 947      *            {@code (index < 0 || index >= tab count)}
 948      *
 949      * @see #addTab
 950      * @see #insertTab
 951      */

 952     public void removeTabAt(int index) {
 953         checkIndex(index);
 954 
 955         Component component = getComponentAt(index);
 956         boolean shouldChangeFocus = false;
 957         int selected = getSelectedIndex();
 958         String oldName = null;
 959 
 960         /* if we're about to remove the visible component */
 961         if (component == visComp) {
 962             shouldChangeFocus = (SwingUtilities.findFocusOwner(visComp) != null);
 963             visComp = null;
 964         }
 965 
 966         if (accessibleContext != null) {
 967             /* if we're removing the selected page */
 968             if (index == selected) {
 969                 /* fire an accessible notification that it's unselected */
 970                 pages.get(index).firePropertyChange(
 971                     AccessibleContext.ACCESSIBLE_STATE_PROPERTY,


1540         if (enabled != oldEnabled) {
1541             revalidate();
1542             repaint();
1543         }
1544     }
1545 
1546     /**
1547      * Sets the component at <code>index</code> to <code>component</code>.
1548      * An internal exception is raised if there is no tab at that index.
1549      *
1550      * @param index the tab index where this component is being placed
1551      * @param component the component for the tab
1552      * @exception IndexOutOfBoundsException if index is out of range
1553      *            {@code (index < 0 || index >= tab count)}
1554      *
1555      * @see #getComponentAt
1556      * @beaninfo
1557      *    attribute: visualUpdate true
1558      *  description: The component at the specified tab index.
1559      */

1560     public void setComponentAt(int index, Component component) {
1561         Page page = pages.get(index);
1562         if (component != page.component) {
1563             boolean shouldChangeFocus = false;
1564 
1565             if (page.component != null) {
1566                 shouldChangeFocus =
1567                     (SwingUtilities.findFocusOwner(page.component) != null);
1568 
1569                 // REMIND(aim): this is really silly;
1570                 // why not if (page.component.getParent() == this) remove(component)
1571                 synchronized(getTreeLock()) {
1572                     int count = getComponentCount();
1573                     Component children[] = getComponents();
1574                     for (int i = 0; i < count; i++) {
1575                         if (children[i] == page.component) {
1576                             super.remove(i);
1577                         }
1578                     }
1579                 }




 316      * @since 1.4
 317      */
 318     public ChangeListener[] getChangeListeners() {
 319         return listenerList.getListeners(ChangeListener.class);
 320     }
 321 
 322     /**
 323      * Sends a {@code ChangeEvent}, with this {@code JTabbedPane} as the source,
 324      * to each registered listener. This method is called each time there is
 325      * a change to either the selected index or the selected tab in the
 326      * {@code JTabbedPane}. Usually, the selected index and selected tab change
 327      * together. However, there are some cases, such as tab addition, where the
 328      * selected index changes and the same tab remains selected. There are other
 329      * cases, such as deleting the selected tab, where the index remains the
 330      * same, but a new tab moves to that index. Events are fired for all of
 331      * these cases.
 332      *
 333      * @see #addChangeListener
 334      * @see EventListenerList
 335      */
 336     @SuppressWarnings("deprecation")
 337     protected void fireStateChanged() {
 338         /* --- Begin code to deal with visibility --- */
 339 
 340         /* This code deals with changing the visibility of components to
 341          * hide and show the contents for the selected tab. It duplicates
 342          * logic already present in BasicTabbedPaneUI, logic that is
 343          * processed during the layout pass. This code exists to allow
 344          * developers to do things that are quite difficult to accomplish
 345          * with the previous model of waiting for the layout pass to process
 346          * visibility changes; such as requesting focus on the new visible
 347          * component.
 348          *
 349          * For the average code, using the typical JTabbedPane methods,
 350          * all visibility changes will now be processed here. However,
 351          * the code in BasicTabbedPaneUI still exists, for the purposes
 352          * of backward compatibility. Therefore, when making changes to
 353          * this code, ensure that the BasicTabbedPaneUI code is kept in
 354          * synch.
 355          */
 356 


 933             // Container.add() interprets -1 as "append", so convert
 934             // the index appropriately to be handled by the vector
 935             insertTab(title, icon, component, null, index == -1? getTabCount() : index);
 936         } else {
 937             super.add(component, constraints, index);
 938         }
 939     }
 940 
 941     /**
 942      * Removes the tab at <code>index</code>.
 943      * After the component associated with <code>index</code> is removed,
 944      * its visibility is reset to true to ensure it will be visible
 945      * if added to other containers.
 946      * @param index the index of the tab to be removed
 947      * @exception IndexOutOfBoundsException if index is out of range
 948      *            {@code (index < 0 || index >= tab count)}
 949      *
 950      * @see #addTab
 951      * @see #insertTab
 952      */
 953     @SuppressWarnings("deprecation")
 954     public void removeTabAt(int index) {
 955         checkIndex(index);
 956 
 957         Component component = getComponentAt(index);
 958         boolean shouldChangeFocus = false;
 959         int selected = getSelectedIndex();
 960         String oldName = null;
 961 
 962         /* if we're about to remove the visible component */
 963         if (component == visComp) {
 964             shouldChangeFocus = (SwingUtilities.findFocusOwner(visComp) != null);
 965             visComp = null;
 966         }
 967 
 968         if (accessibleContext != null) {
 969             /* if we're removing the selected page */
 970             if (index == selected) {
 971                 /* fire an accessible notification that it's unselected */
 972                 pages.get(index).firePropertyChange(
 973                     AccessibleContext.ACCESSIBLE_STATE_PROPERTY,


1542         if (enabled != oldEnabled) {
1543             revalidate();
1544             repaint();
1545         }
1546     }
1547 
1548     /**
1549      * Sets the component at <code>index</code> to <code>component</code>.
1550      * An internal exception is raised if there is no tab at that index.
1551      *
1552      * @param index the tab index where this component is being placed
1553      * @param component the component for the tab
1554      * @exception IndexOutOfBoundsException if index is out of range
1555      *            {@code (index < 0 || index >= tab count)}
1556      *
1557      * @see #getComponentAt
1558      * @beaninfo
1559      *    attribute: visualUpdate true
1560      *  description: The component at the specified tab index.
1561      */
1562     @SuppressWarnings("deprecation")
1563     public void setComponentAt(int index, Component component) {
1564         Page page = pages.get(index);
1565         if (component != page.component) {
1566             boolean shouldChangeFocus = false;
1567 
1568             if (page.component != null) {
1569                 shouldChangeFocus =
1570                     (SwingUtilities.findFocusOwner(page.component) != null);
1571 
1572                 // REMIND(aim): this is really silly;
1573                 // why not if (page.component.getParent() == this) remove(component)
1574                 synchronized(getTreeLock()) {
1575                     int count = getComponentCount();
1576                     Component children[] = getComponents();
1577                     for (int i = 0; i < count; i++) {
1578                         if (children[i] == page.component) {
1579                             super.remove(i);
1580                         }
1581                     }
1582                 }


< prev index next >