1 /* TabbedPaneDemo.java -- Demonstrates JTabbedPane
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.swing;
41 import java.awt.BorderLayout;
42 import java.awt.Color;
43 import java.awt.GridLayout;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.MouseAdapter;
47 import java.awt.event.MouseEvent;
49 import javax.swing.JButton;
50 import javax.swing.JComponent;
51 import javax.swing.JFrame;
52 import javax.swing.JLabel;
53 import javax.swing.JMenu;
54 import javax.swing.JMenuItem;
55 import javax.swing.JPanel;
56 import javax.swing.JPopupMenu;
57 import javax.swing.JScrollPane;
58 import javax.swing.JTabbedPane;
59 import javax.swing.JTextArea;
60 import javax.swing.SwingConstants;
61 import javax.swing.SwingUtilities;
63 public class TabbedPaneDemo
65 implements ActionListener
67 static Color[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
68 Color.MAGENTA, Color.ORANGE, Color.PINK,
69 Color.ORANGE, Color.RED, Color.BLUE, Color.YELLOW
77 private void createContent()
79 JPanel p = new JPanel();
80 p.setLayout(new GridLayout(1, 1));
83 JTabbedPane tp = createTabbedPane(SwingConstants.TOP, "tab", COUNT);
86 final JPopupMenu popup = new JPopupMenu();
88 JMenu menu = new JMenu("tab placement");
89 menu.add(createPlacementChangingMenuItem("top",
93 menu.add(createPlacementChangingMenuItem("bottom",
94 SwingConstants.BOTTOM,
97 menu.add(createPlacementChangingMenuItem("left",
101 menu.add(createPlacementChangingMenuItem("right",
102 SwingConstants.RIGHT,
106 menu = new JMenu("tab layout");
107 menu.add(createLayoutPolicyChangingMenuItem("wrapping tabs",
108 JTabbedPane.WRAP_TAB_LAYOUT,
111 menu.add(createLayoutPolicyChangingMenuItem("scrolling tabs",
112 JTabbedPane.SCROLL_TAB_LAYOUT,
116 tp.addMouseListener(new MouseAdapter()
118 public void mousePressed(MouseEvent e) {
122 public void mouseReleased(MouseEvent e) {
126 void showPopup(MouseEvent e) {
127 if (e.isPopupTrigger()) {
128 popup.show(e.getComponent(), e.getX(), e.getY());
133 setLayout(new BorderLayout());
134 add(p, BorderLayout.CENTER);
138 private JMenuItem createPlacementChangingMenuItem(String t,
140 final JTabbedPane dst)
142 JMenuItem item = new JMenuItem(t);
144 item.addActionListener(new ActionListener()
146 public void actionPerformed(ActionEvent ae)
148 dst.setTabPlacement(v);
155 private JMenuItem createLayoutPolicyChangingMenuItem(String t,
157 final JTabbedPane dst)
159 JMenuItem item = new JMenuItem(t);
161 item.addActionListener(new ActionListener()
163 public void actionPerformed(ActionEvent ae)
165 dst.setTabLayoutPolicy(v);
172 private JTabbedPane createTabbedPane(int direction, String name, int count)
174 JTabbedPane pane = new JTabbedPane(direction);
176 for(int i = 0; i< count; i++)
178 pane.addTab(name + " " + i, createTabContent(name + " " + i));
179 if (Math.random() >= 0.75)
180 pane.setEnabledAt(i, false);
186 private JPanel createTabContent(String name)
189 JPanel panel = new JPanel();
190 panel.add(new JLabel(name));
191 panel.add(new JButton(name));
192 panel.add(new JScrollPane(ta = new JTextArea(5, 5)));
194 ta.setBackground(colors[(int) (Math.random() * colors.length)]);
199 public void actionPerformed(ActionEvent e)
201 if (e.getActionCommand().equals("CLOSE"))
208 * When the demo is run independently, the frame is displayed, so we should
209 * initialise the content panel (including the demo content and a close
210 * button). But when the demo is run as part of the Swing activity board,
211 * only the demo content panel is used, the frame itself is never displayed,
212 * so we can avoid this step.
214 void initFrameContent()
216 JPanel closePanel = new JPanel();
217 JButton closeButton = new JButton("Close");
218 closeButton.setActionCommand("CLOSE");
219 closeButton.addActionListener(this);
220 closePanel.add(closeButton);
221 add(closePanel, BorderLayout.SOUTH);
224 public static void main(String[] args)
226 SwingUtilities.invokeLater
231 TabbedPaneDemo app = new TabbedPaneDemo();
232 app.initFrameContent();
233 JFrame frame = new JFrame("TabbedPane Demo");
234 frame.getContentPane().add(app);
236 frame.setVisible(true);
242 * Returns a DemoFactory that creates a TabbedPaneDemo.
244 * @return a DemoFactory that creates a TabbedPaneDemo
246 public static DemoFactory createDemoFactory()
248 return new DemoFactory()
250 public JComponent createDemo()
252 return new TabbedPaneDemo();