The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
< Day Day Up > |
Here's a snapshot of an application that uses a CardLayout [10] to switch between two panels. [10] CardLayout API documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/CardLayout.html.
The CardLayout class helps you manage two or more components (usually JPanel instances) that share the same display space. When using it, you need to provide a way for the user to choose between the components. CardLayoutDemo uses a combo box for this purpose. An easier but less flexible way to accomplish the same task is to use a tabbed pane instead. [12] Figure 15 is a picture of a tabbed pane version of Figure 14. [12] See How to Use Tabbed Panes (page 382) for more information. Figure 15. The GUI reworked to use a tabbed pane instead of CardLayout .
Figure 14. Two simple GUIs that use CardLayout .
Conceptually, each component a CardLayout manages is like a playing card in a deck, where only the top card is visible at any time. You can choose the card that's showing in any of the following ways:
The following code from CardLayoutDemo.java creates the CardLayout and the components it manages. //Where instance variables are declared: JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; // Where the components controlled by the CardLayout are initialized: // Create the "cards". JPanel card1 = new JPanel(); ... JPanel card2 = new JPanel(); ... //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); When you add a component to a container that CardLayout manages, you must specify a string that identifies it. For example, in the preceding code the first panel has the string "JPanel with JButtons" and the second has the string "JPanel with JTextField" . In CardLayoutDemo , those strings are also used in the combo box. To choose which component a CardLayout shows, you need some additional code. The following shows how the example program does this: //Where the GUI is assembled: //Put the JComboBox in a JPanel to get a nicer look. JPanel comboBoxPane = new JPanel(); //use FlowLayout String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JComboBox cb = new JComboBox(comboBoxItems); cb.setEditable(false); cb.addItemListener(this); comboBoxPane.add(cb); ... pane.add(comboBoxPane, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER); ... public void itemStateChanged(ItemEvent evt) { CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)evt.getItem()); } The code demonstrates that you can use the CardLayout show method to set the currently showing component. The first argument to show is the container CardLayout controls ”that is, the container of the components CardLayout manages. The second argument is the string that identifies the component to show. This is the same string used when adding the component to the container. The CardLayout API
Table 6 lists the commonly used CardLayout methods that let you choose a component. You can find the CardLayout API documentation online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/CardLayout.html. Table 6. CardLayout Methods
Examples That Use CardLayout
Only one example in this book uses CardLayout : CardLayoutDemo . Generally, our examples use tabbed panes instead of CardLayout , since they conveniently provide a nice GUI for the same functionality. |
< Day Day Up > |