Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
The JTabbedPane allows many components to occupy the same area. Although it it not an actual Layout, it performs a similar task.
TabbedPaneDemo Example (Run Applet)
import java.awt.*; import javax.swing.*; public class TabbedPaneDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { public View() { super(); // make a tabbed pane and force it to occupy the whole of the applet's panel final JTabbedPane tabbedPane = new JTabbedPane(); this.setLayout(new GridLayout(1, 1)); this.add(tabbedPane); final ImageIcon red_icon = new ImageIcon(getClass().getClassLoader().getResource("images/red_bullet.gif")); final ImageIcon green_icon = new ImageIcon(getClass().getClassLoader().getResource("images/green_bullet.gif")); final ImageIcon blue_icon = new ImageIcon(getClass().getClassLoader().getResource("images/blue_bullet.gif")); final JPanel red_panel = new JPanel(); final JPanel green_panel = new JPanel(); final JPanel blue_panel = new JPanel(); red_panel.setBackground(Color.red); red_panel.add(new JLabel("Red panel text")); green_panel.setBackground(Color.green); green_panel.add(new JButton("Green panel button")); blue_panel.setBackground(Color.blue); blue_panel.add(new JLabel(new ImageIcon(getClass().getClassLoader().getResource("images/koala.gif")))); // add the tabs to the tabbedPane tabbedPane.addTab("Red", red_icon, red_panel, "Tooltip for red panel"); tabbedPane.addTab("Green", green_icon, green_panel, "Tooltip for green panel"); tabbedPane.addTab("Blue", blue_icon, blue_panel, "Tooltip for blue panel"); } } }
By setting the View to be a TabbedPane, the applet's containing panel acts as a TabbedPane. This is shown in the example below.
ContentPane can also Extend for the other Panes that we shall look at in these notes.
TabbedPane_Extends_Demo Example (Run Applet)
import java.awt.*; import javax.swing.*; public class TabbedPane_Extends_Demo extends JApplet { @Override public void init() { this.setContentPane(new View()); } // note that View extends JTabbedPane public class View extends JTabbedPane { public View() { super(); final ImageIcon red_icon = new ImageIcon(getClass().getClassLoader().getResource("images/red_bullet.gif")); final ImageIcon green_icon = new ImageIcon(getClass().getClassLoader().getResource("images/green_bullet.gif")); final ImageIcon blue_icon = new ImageIcon(getClass().getClassLoader().getResource("images/blue_bullet.gif")); final JPanel red_panel = new JPanel(); final JPanel green_panel = new JPanel(); final JPanel blue_panel = new JPanel(); red_panel.setBackground(Color.red); red_panel.add(new JLabel("Red panel text")); green_panel.setBackground(Color.green); green_panel.add(new JButton("Green panel button")); blue_panel.setBackground(Color.blue); blue_panel.add(new JLabel(new ImageIcon(getClass().getClassLoader().getResource("images/koala.gif")))); this.addTab("Red", red_icon, red_panel, "Tooltip for red panel"); this.addTab("Green", green_icon, green_panel, "Tooltip for green panel"); this.addTab("Blue", blue_icon, blue_panel, "Tooltip for blue panel"); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.