< prev index next >

src/java.desktop/share/classes/java/awt/CardLayout.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.util.Hashtable;
  29 import java.util.Vector;
  30 import java.util.Enumeration;
  31 
  32 import java.io.Serializable;
  33 import java.io.ObjectInputStream;
  34 import java.io.ObjectOutputStream;
  35 import java.io.ObjectStreamField;
  36 import java.io.IOException;
  37 
  38 /**
  39  * A <code>CardLayout</code> object is a layout manager for a
  40  * container. It treats each component in the container as a card.
  41  * Only one card is visible at a time, and the container acts as
  42  * a stack of cards. The first component added to a
  43  * <code>CardLayout</code> object is the visible component when the
  44  * container is first displayed.
  45  * <p>
  46  * The ordering of cards is determined by the container's own internal
  47  * ordering of its component objects. <code>CardLayout</code>
  48  * defines a set of methods that allow an application to flip
  49  * through these cards sequentially, or to show a specified card.
  50  * The {@link CardLayout#addLayoutComponent}
  51  * method can be used to associate a string identifier with a given card
  52  * for fast random access.
  53  *
  54  * @author      Arthur van Hoff
  55  * @see         java.awt.Container
  56  * @since       1.0
  57  */
  58 
  59 public class CardLayout implements LayoutManager2,
  60                                    Serializable {
  61 
  62     private static final long serialVersionUID = -4328196481005934313L;
  63 
  64     /*
  65      * This creates a Vector to store associated
  66      * pairs of components and their names.
  67      * @see java.util.Vector


 171      * @see       java.awt.CardLayout#setVgap(int)
 172      * @see       java.awt.CardLayout#getHgap()
 173      */
 174     public int getVgap() {
 175         return vgap;
 176     }
 177 
 178     /**
 179      * Sets the vertical gap between components.
 180      * @param     vgap the vertical gap between components.
 181      * @see       java.awt.CardLayout#getVgap()
 182      * @see       java.awt.CardLayout#setHgap(int)
 183      * @since     1.1
 184      */
 185     public void setVgap(int vgap) {
 186         this.vgap = vgap;
 187     }
 188 
 189     /**
 190      * Adds the specified component to this card layout's internal
 191      * table of names. The object specified by <code>constraints</code>
 192      * must be a string. The card layout stores this string as a key-value
 193      * pair that can be used for random access to a particular card.
 194      * By calling the <code>show</code> method, an application can
 195      * display the component with the specified name.
 196      * @param     comp          the component to be added.
 197      * @param     constraints   a tag that identifies a particular
 198      *                                        card in the layout.
 199      * @see       java.awt.CardLayout#show(java.awt.Container, java.lang.String)
 200      * @exception  IllegalArgumentException  if the constraint is not a string.
 201      */
 202     public void addLayoutComponent(Component comp, Object constraints) {
 203       synchronized (comp.getTreeLock()) {
 204           if (constraints == null){
 205               constraints = "";
 206           }
 207         if (constraints instanceof String) {
 208             addLayoutComponent((String)constraints, comp);
 209         } else {
 210             throw new IllegalArgumentException("cannot add to layout: constraint must be a string");
 211         }
 212       }
 213     }
 214 
 215     /**
 216      * @deprecated   replaced by
 217      *      <code>addLayoutComponent(Component, Object)</code>.
 218      */
 219     @Deprecated
 220     public void addLayoutComponent(String name, Component comp) {
 221         synchronized (comp.getTreeLock()) {
 222             if (!vector.isEmpty()) {
 223                 comp.setVisible(false);
 224             }
 225             for (int i=0; i < vector.size(); i++) {
 226                 if ((vector.get(i)).name.equals(name)) {
 227                     (vector.get(i)).comp = comp;
 228                     return;
 229                 }
 230             }
 231             vector.add(new Card(name, comp));
 232         }
 233     }
 234 
 235     /**
 236      * Removes the specified component from the layout.
 237      * If the card was visible on top, the next card underneath it is shown.


 348      * Returns the alignment along the y axis.  This specifies how
 349      * the component would like to be aligned relative to other
 350      * components.  The value should be a number between 0 and 1
 351      * where 0 represents alignment along the origin, 1 is aligned
 352      * the furthest away from the origin, 0.5 is centered, etc.
 353      */
 354     public float getLayoutAlignmentY(Container parent) {
 355         return 0.5f;
 356     }
 357 
 358     /**
 359      * Invalidates the layout, indicating that if the layout manager
 360      * has cached information it should be discarded.
 361      */
 362     public void invalidateLayout(Container target) {
 363     }
 364 
 365     /**
 366      * Lays out the specified container using this card layout.
 367      * <p>
 368      * Each component in the <code>parent</code> container is reshaped
 369      * to be the size of the container, minus space for surrounding
 370      * insets, horizontal gaps, and vertical gaps.
 371      *
 372      * @param     parent the parent container in which to do the layout
 373      * @see       java.awt.Container#doLayout
 374      */
 375     public void layoutContainer(Container parent) {
 376         synchronized (parent.getTreeLock()) {
 377             Insets insets = parent.getInsets();
 378             int ncomponents = parent.getComponentCount();
 379             Component comp = null;
 380             boolean currentFound = false;
 381 
 382             for (int i = 0 ; i < ncomponents ; i++) {
 383                 comp = parent.getComponent(i);
 384                 comp.setBounds(hgap + insets.left, vgap + insets.top,
 385                                parent.width - (hgap*2 + insets.left + insets.right),
 386                                parent.height - (vgap*2 + insets.top + insets.bottom));
 387                 if (comp.isVisible()) {
 388                     currentFound = true;


 498         synchronized (parent.getTreeLock()) {
 499             checkLayout(parent);
 500             int ncomponents = parent.getComponentCount();
 501             for (int i = 0 ; i < ncomponents ; i++) {
 502                 Component comp = parent.getComponent(i);
 503                 if (comp.isVisible()) {
 504                     comp.setVisible(false);
 505                     break;
 506                 }
 507             }
 508             if (ncomponents > 0) {
 509                 currentCard = ncomponents - 1;
 510                 parent.getComponent(currentCard).setVisible(true);
 511                 parent.validate();
 512             }
 513         }
 514     }
 515 
 516     /**
 517      * Flips to the component that was added to this layout with the
 518      * specified <code>name</code>, using <code>addLayoutComponent</code>.
 519      * If no such component exists, then nothing happens.
 520      * @param     parent   the parent container in which to do the layout
 521      * @param     name     the component name
 522      * @see       java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
 523      */
 524     public void show(Container parent, String name) {
 525         synchronized (parent.getTreeLock()) {
 526             checkLayout(parent);
 527             Component next = null;
 528             int ncomponents = vector.size();
 529             for (int i = 0; i < ncomponents; i++) {
 530                 Card card = vector.get(i);
 531                 if (card.name.equals(name)) {
 532                     next = card.comp;
 533                     currentCard = i;
 534                     break;
 535                 }
 536             }
 537             if ((next != null) && !next.isVisible()) {
 538                 ncomponents = parent.getComponentCount();




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.util.Hashtable;
  29 import java.util.Vector;
  30 import java.util.Enumeration;
  31 
  32 import java.io.Serializable;
  33 import java.io.ObjectInputStream;
  34 import java.io.ObjectOutputStream;
  35 import java.io.ObjectStreamField;
  36 import java.io.IOException;
  37 
  38 /**
  39  * A {@code CardLayout} object is a layout manager for a
  40  * container. It treats each component in the container as a card.
  41  * Only one card is visible at a time, and the container acts as
  42  * a stack of cards. The first component added to a
  43  * {@code CardLayout} object is the visible component when the
  44  * container is first displayed.
  45  * <p>
  46  * The ordering of cards is determined by the container's own internal
  47  * ordering of its component objects. {@code CardLayout}
  48  * defines a set of methods that allow an application to flip
  49  * through these cards sequentially, or to show a specified card.
  50  * The {@link CardLayout#addLayoutComponent}
  51  * method can be used to associate a string identifier with a given card
  52  * for fast random access.
  53  *
  54  * @author      Arthur van Hoff
  55  * @see         java.awt.Container
  56  * @since       1.0
  57  */
  58 
  59 public class CardLayout implements LayoutManager2,
  60                                    Serializable {
  61 
  62     private static final long serialVersionUID = -4328196481005934313L;
  63 
  64     /*
  65      * This creates a Vector to store associated
  66      * pairs of components and their names.
  67      * @see java.util.Vector


 171      * @see       java.awt.CardLayout#setVgap(int)
 172      * @see       java.awt.CardLayout#getHgap()
 173      */
 174     public int getVgap() {
 175         return vgap;
 176     }
 177 
 178     /**
 179      * Sets the vertical gap between components.
 180      * @param     vgap the vertical gap between components.
 181      * @see       java.awt.CardLayout#getVgap()
 182      * @see       java.awt.CardLayout#setHgap(int)
 183      * @since     1.1
 184      */
 185     public void setVgap(int vgap) {
 186         this.vgap = vgap;
 187     }
 188 
 189     /**
 190      * Adds the specified component to this card layout's internal
 191      * table of names. The object specified by {@code constraints}
 192      * must be a string. The card layout stores this string as a key-value
 193      * pair that can be used for random access to a particular card.
 194      * By calling the {@code show} method, an application can
 195      * display the component with the specified name.
 196      * @param     comp          the component to be added.
 197      * @param     constraints   a tag that identifies a particular
 198      *                                        card in the layout.
 199      * @see       java.awt.CardLayout#show(java.awt.Container, java.lang.String)
 200      * @exception  IllegalArgumentException  if the constraint is not a string.
 201      */
 202     public void addLayoutComponent(Component comp, Object constraints) {
 203       synchronized (comp.getTreeLock()) {
 204           if (constraints == null){
 205               constraints = "";
 206           }
 207         if (constraints instanceof String) {
 208             addLayoutComponent((String)constraints, comp);
 209         } else {
 210             throw new IllegalArgumentException("cannot add to layout: constraint must be a string");
 211         }
 212       }
 213     }
 214 
 215     /**
 216      * @deprecated   replaced by
 217      *      {@code addLayoutComponent(Component, Object)}.
 218      */
 219     @Deprecated
 220     public void addLayoutComponent(String name, Component comp) {
 221         synchronized (comp.getTreeLock()) {
 222             if (!vector.isEmpty()) {
 223                 comp.setVisible(false);
 224             }
 225             for (int i=0; i < vector.size(); i++) {
 226                 if ((vector.get(i)).name.equals(name)) {
 227                     (vector.get(i)).comp = comp;
 228                     return;
 229                 }
 230             }
 231             vector.add(new Card(name, comp));
 232         }
 233     }
 234 
 235     /**
 236      * Removes the specified component from the layout.
 237      * If the card was visible on top, the next card underneath it is shown.


 348      * Returns the alignment along the y axis.  This specifies how
 349      * the component would like to be aligned relative to other
 350      * components.  The value should be a number between 0 and 1
 351      * where 0 represents alignment along the origin, 1 is aligned
 352      * the furthest away from the origin, 0.5 is centered, etc.
 353      */
 354     public float getLayoutAlignmentY(Container parent) {
 355         return 0.5f;
 356     }
 357 
 358     /**
 359      * Invalidates the layout, indicating that if the layout manager
 360      * has cached information it should be discarded.
 361      */
 362     public void invalidateLayout(Container target) {
 363     }
 364 
 365     /**
 366      * Lays out the specified container using this card layout.
 367      * <p>
 368      * Each component in the {@code parent} container is reshaped
 369      * to be the size of the container, minus space for surrounding
 370      * insets, horizontal gaps, and vertical gaps.
 371      *
 372      * @param     parent the parent container in which to do the layout
 373      * @see       java.awt.Container#doLayout
 374      */
 375     public void layoutContainer(Container parent) {
 376         synchronized (parent.getTreeLock()) {
 377             Insets insets = parent.getInsets();
 378             int ncomponents = parent.getComponentCount();
 379             Component comp = null;
 380             boolean currentFound = false;
 381 
 382             for (int i = 0 ; i < ncomponents ; i++) {
 383                 comp = parent.getComponent(i);
 384                 comp.setBounds(hgap + insets.left, vgap + insets.top,
 385                                parent.width - (hgap*2 + insets.left + insets.right),
 386                                parent.height - (vgap*2 + insets.top + insets.bottom));
 387                 if (comp.isVisible()) {
 388                     currentFound = true;


 498         synchronized (parent.getTreeLock()) {
 499             checkLayout(parent);
 500             int ncomponents = parent.getComponentCount();
 501             for (int i = 0 ; i < ncomponents ; i++) {
 502                 Component comp = parent.getComponent(i);
 503                 if (comp.isVisible()) {
 504                     comp.setVisible(false);
 505                     break;
 506                 }
 507             }
 508             if (ncomponents > 0) {
 509                 currentCard = ncomponents - 1;
 510                 parent.getComponent(currentCard).setVisible(true);
 511                 parent.validate();
 512             }
 513         }
 514     }
 515 
 516     /**
 517      * Flips to the component that was added to this layout with the
 518      * specified {@code name}, using {@code addLayoutComponent}.
 519      * If no such component exists, then nothing happens.
 520      * @param     parent   the parent container in which to do the layout
 521      * @param     name     the component name
 522      * @see       java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
 523      */
 524     public void show(Container parent, String name) {
 525         synchronized (parent.getTreeLock()) {
 526             checkLayout(parent);
 527             Component next = null;
 528             int ncomponents = vector.size();
 529             for (int i = 0; i < ncomponents; i++) {
 530                 Card card = vector.get(i);
 531                 if (card.name.equals(name)) {
 532                     next = card.comp;
 533                     currentCard = i;
 534                     break;
 535                 }
 536             }
 537             if ((next != null) && !next.isVisible()) {
 538                 ncomponents = parent.getComponentCount();


< prev index next >