Section 17.12. The JTabbedPane Class

17.12. The JTabbedPane Class

If you've ever right-clicked on the desktop to set your Display Properties in Windows, you already know what a JTabbedPane is. It's a container with labeled tabs (e.g., Themes, Screen Saver, Appearance). When you click on a tab, a new set of controls is shown in the body of the JTabbedPane. In Swing, JTabbedPane is simply a specialized container.

Each tab has a name. To add a tab to the JTabbedPane, simply call addTab( ). You'll need to specify the name of the tab as well as a component that supplies the tab's contents. Typically, it's a container holding other components.

Even though the JTabbedPane only shows one set of components at a time, be aware that all the components on all the pages are alive and in memory at one time. If you have components that hog processor time or memory, try to put them into some "sleep" state when they are not showing.

The following example shows how to create a JTabbedPane. It adds standard Swing components to a tab named Controls. The second tab is filled with a scrollable image, which was presented in the previous examples.

     //file:     import java.awt.*;     import java.awt.event.*;     import javax.swing.*;     import javax.swing.border.*;     public class TabbedPaneFrame {       public static void main(String[] args)       {         JFrame frame = new JFrame("TabbedPaneFrame");         JTabbedPane tabby = new JTabbedPane(  );         // create the controls pane         JPanel controls = new JPanel(  );         controls.add(new JLabel("Service:"));         JList list = new JList(             new String[] { "Web server", "FTP server" });         list.setBorder(BorderFactory.createEtchedBorder(  ));         controls.add(list);         controls.add(new JButton("Start"));         // create an image pane         String filename = "Piazza di Spagna.jpg";         JLabel image = new JLabel( new ImageIcon(filename) );         JComponent picture = new JScrollPane(image);         tabby.addTab("Controls", controls);         tabby.addTab("Picture", picture);         frame.getContentPane(  ).add(tabby);         frame.setSize(200, 200);         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );         frame.setVisible(true);       }     } 

The code isn't especially fancy, but the result is an impressive-looking user interface. The first tab is a JPanel that contains some other components, including a JList with an etched border. The second tab simply contains the JLabel with ImageIcon wrapped in a JScrollPane. The running example is shown in Figure 17-11.

Our example has only two tabs, and they fit quite easily, but in a realistic application it is easy to run out of room. By default, when there are too many tabs to display in a single row, JTabbedPane automatically wraps them into additional rows. This behavior fits with the tab notion quite well, giving the appearance of a filing cabinet, but it also necessitates that when you select a tab from the back row, the tabs must be rearranged to bring the selected tab to the foreground. Many users find this confusing, and it violates a principal of user interface design that says that controls should remain in the same location. Alternatively the tabbed pane can be configured to use a single, scrolling row of tabs by specifying a scrolling tab layout policy like this:

Figure 17-11. Using a tabbed pane

     setTabLayoutPolicy( JTabbedPane.SCROLL_TAB_LAYOUT );

    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: