JTabbedPane

     

This component provides a way to access an arbitrary set of panels. It capitalizes on CardLayout shown earlier, and offers small tabs atop your JPanels to offer the user an easy way to switch around. This is used frequently in office productivity applications to let the user specify preferences and so forth. It is handy and surprisingly easy to use.

TabbedPaneDemo.java

 

 package net.javagarage.demo.ui; /**<p>  * Shows a JTabbedPane, which allows you to  * choose between pages with a tab, like  * paper manila folders.  *  * </p>  * @author Eben Hewitt  **/ import javax.swing.JTabbedPane; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JComponent; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.SwingUtilities; import java.net.MalformedURLException; import java.net.URL; public class TabbedPaneDemo extends JPanel { static URL imageURL; public TabbedPaneDemo() { //remember that the call to super() //MUST be first call in constructor super(new GridLayout(1, 1)); //create the tabbed pane JTabbedPane tabbedPane = new JTabbedPane(); /*remember that the present working directory will be above your classes directory. so if this class is in C:\projects\garage\net\javagarage\etc... Put the image you want in C:\projects\garage. */ String imagePath = System.getProperty("user.dir") + "\kitty.gif"; //get the image that will prefix each tab ImageIcon icon = new ImageIcon(imagePath); //create a new panel using our custom //utility class JComponent noodlePanel = new TabPanel("prissy one").getPanel(); //add a tab to the panel tabbedPane.addTab("Noodlehead", icon, noodlePanel); JComponent doodlePanel = new TabPanel("princess").getPanel(); tabbedPane.addTab("Doodlehead", icon, doodlePanel); JComponent mrPanel = new TabPanel("the boy kitty").getPanel(); tabbedPane.addTab("Mr. Apache Tomcat", icon, mrPanel); //add the tabbed pane add(tabbedPane); //scrolling tabs makes small navigation arrows for //use when all tabs do not fit on the screen tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_ LAYOUT); } //Makes the icon of the image file we provide, to //include on each tab. If no file is specified or //found, null is returned, and no icon is included. protected static ImageIcon createIcon(String path) { try { imageURL = new URL(path); return new ImageIcon(imageURL); } catch (MalformedURLException me){ System.err.println("This is not an acceptable URL: " + path + " \n" + me.getMessage()); return null; } } /**  * Put the application on the event-handling thread  * and start it up.  */ private static void launch() { //must be called first, and static-ly JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("Tabbed Pane App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create a new instance of this class and //make the contents visible JComponent newContentPane = new TabbedPaneDemo(); newContentPane.setOpaque(true); frame.getContentPane().add(newContentPane, BorderLayout.CENTER); //show it frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //run the app as recommended SwingUtilities.invokeLater(new Runnable() { public void run() { launch(); } }); } } /**  * Class to encapsulate the business of creating panels  * that hold the content of the application (in this  * case just a line of text). The tabs tack on to the  * top of these panels.  */ class TabPanel { JPanel panel; JLabel filler; public TabPanel(String text){ panel = new JPanel(false); filler = new JLabel(text); filler.setHorizontalAlignment(JLabel.CENTER); panel.setPreferredSize(new Dimension(250,100)); panel.add(filler); } public JComponent getPanel() { return panel; } } //end TabbedPaneDemo 

} //end components .stepInto()



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net