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:\projectsgarage
etjavagarageetc...
Put the image you want in C:\projectsgarage.
*/

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 + " 
" + 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()

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.135.36