In the front of this chapter, we introduced the old-fashioned cardlayout and noticed how we managed all of our card switches. Interestingly, someone now thinks it's a good design. Fortunately, swing has patched it up with JTabbedPane, and the JTabbedPane handles the cards, switches, and everything else. Comparing CardLayout and JTabbedPane, we'll find amazing differences.
The following program example is interesting because it leverages the design of the previous example. They are all built as JPanel derivatives, so the program will place each of the preceding examples into its own JTabbedPane pane. We will see that the program made using Rtti is very small and exquisite:
: Tabbed.java//Using tabbed panes package c13.swing;
Import java.awt.*;
Import javax.swing.*;
Import javax.swing.border.*; public class Tabbed extends JPanel {static object[][] Q = {{"Felix", Borders.class}, {"The Professor", Butt
Ons.class}, {"Rock Bottom", Buttongroups.class}, {"Theodore", Faces.class}, {"Simon", Menus.class},
{"Alvin", Popup.class}, {"Tom", Listcombo.class}, {"Jerry", Progress.class}, {"Bugs", Trees.class},
{"Daffy", Table.class},};
Static JPanel Makepanel (Class c) {String title = C.getname ();
title = Title.substring (Title.lastindexof ('. ') + 1);
JPanel sp = null;
try {sp = (JPanel) c.newinstance ();
catch (Exception e) {System.out.println (e);
} sp.setborder (new Titledborder (title));
return SP;
Public tabbed () {setlayout (New BorderLayout ());
JTabbedPane tabbed = new JTabbedPane ();
for (int i = 0; i < q.length; i++)Tabbed.addtab ((String) q[i][0], Makepanel ((Class) q[i][1));
Add (tabbed, borderlayout.center);
Tabbed.setselectedindex (Q.LENGTH/2);
public static void Main (String args[]) {show.inframe (New tabbed (), 460,350); }
} ///:~
Furthermore, we can note the array construction style used: The first element is a string that is placed on the card, and the second element is the JPanel class that will be displayed on the corresponding pane. In the tabbed () builder, we can see that two important JTabbedPane methods are used: AddTab () Inserts a new pane, setSelectedIndex () selects a pane and starts with it. (a pane selected in the middle shows that we don't have to start with the first pane).
When we call the AddTab () method, we provide it with a card string and some components (that is, an AWT component, not a jcomponent from AWT). This component will be displayed in the pane. Once we do that, there is no need for more management--jtabbedpane will handle anything else for us.
The Makepanel () method takes the class class object we want to create and newinstance () to create and sculpt as JPanel (of course, assuming those classes are classes that must inherit from JPanel, unless used in this section for the structure of the program example). It adds a titledborder that includes the class name and returns the result, to be used as a jpanel in AddTab ().
When we run the program, we find that if there are too many cards and a line is filled, JTabbedPane automatically piles them up.