In this article, you'll take a look at the CardLayout layout manager and JTabbedPane component of the Swing component set. The introduction of the JTabbedPane component in the Java 2 Platform, Standard Edition (J2SE) 1.1 practically made the need for CardLayout obsolete, but it's still there and not deprecated, so let's take a look at the CardLayout layout manager. The CardLayout layout manager is not really a layout manager as most programmers think of one. What it does is let you define any number of "cards" containing, typically, a logically related collection of components. Figure 1 shows a typical card-based configuration window, one that wasn't necessarily implemented using Java technology but could easily be. What CardLayout provides you with is the areas marked History, Saved Forms, and Passwords, including what appears for each selected tab. It doesn't provide the means to swap cards, only to display each one. The following program demonstrates the use of CardLayout with five cards containing a single button each. You may capture the code displayed above in the text version of this Newsletter. Notice the add() method used to add cards to a CardLayout . As with the more familiar BorderLayout , you need to use the constraints variety to pass a card name -- in addition to the card itself -- to the layout manager. Later, you can refer to your cards by name instead of by number, for example. Also note that a CardLayout variable was created. Typically, you simply have a new SomeLayoutManager() method call within the brackets of your setLayout() call. But when using CardLayout , you must refer to your CardLayout object after you have created it to change cards. This is why you need the variable to keep a reference to it. To demonstrate the ability to cycle through the cards, this program was made to listen for action from any of the buttons; the next card is displayed whenever you click on one of the buttons. The next()
method tells the application to show the next card under its control for the container. When it reaches the last card and you ask it to show the next() card again, it simply cycles back to the beginning. See Figure 2. You probably expected to see a full-fledged rendering of the five different cards, as in Figure 1, including the clickable tabs to select any one of the cards. Unfortunately, CardLayout does not do this for you. It is just a layout manager. You must either implement the tabs and card outlines yourself or use the JTabbedPane container. The following program expands on the previous example to demonstrate the usage of JTabbedPane : You may capture the code displayed above in the text version of this Newsletter. Each card is added with the addTab () method, instead of the plain add() method. Also, notice that the event-handling code to move between cards is automatically performed by the JTabbedPane . Because of this added value of JTabbedPane , you'll find yourself almost never using CardLayout . Figure 3 shows the output of the program's new and improved version. The JTabbedPane has many additional options. For instance, it supports the display of images on the tabs and tool tips when the mouse is rested over a tab with: addTab (String title, Icon icon, Component component) addTab (String title, Icon icon, Component component, String tip) You can also move the tabs to different sides of the container with setTabbedPlacement() using one of the class constants -- TOP, BOTTOM, LEFT, and RIGHT -- to determine the side to place them on. One other option says what to do with the tabs when they are too wide for one line. Using setTabLayoutPolicy() , you can have them wrap to multiple levels with the class constant WRAP_TAB_LAYOUT. Or you can have them scroll on one line with SCROLL_TAB_LAYOUT. Figures 4 and 5 show the two different layout policies. Keep in mind that what goes on each tab is just one component. So if you want to create a tab like that in Figure 1, you'd have to place the components within their own container (JPanel ) with its own layout manager before placing them within the tab of the JTabbedPane . |