1 /* ProgressBarDemo.java -- A demonstration of JProgressBars
2 Copyright (C) 2005 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.event.ActionEvent;
42 import java.awt.event.ActionListener;
44 import javax.swing.Box;
45 import javax.swing.BoxLayout;
46 import javax.swing.JButton;
47 import javax.swing.JComponent;
48 import javax.swing.JFrame;
49 import javax.swing.JPanel;
50 import javax.swing.JProgressBar;
51 import javax.swing.JSlider;
52 import javax.swing.SwingUtilities;
53 import javax.swing.event.ChangeEvent;
54 import javax.swing.event.ChangeListener;
56 public class ProgressBarDemo
58 implements ActionListener
62 * Creates a new ProgressBarDemo window with the specified title.
70 void initFrameContent()
72 JPanel closePanel = new JPanel();
73 JButton closeButton = new JButton("Close");
74 closeButton.setActionCommand("CLOSE");
75 closeButton.addActionListener(this);
76 closePanel.add(closeButton);
80 private void createContent()
82 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
83 JPanel horizontalProgressBar = createHorizontalProgressBar();
84 add(horizontalProgressBar);
85 add(Box.createVerticalStrut(10));
86 JPanel verticalProgressBar = createVerticalProgressBar();
87 add(verticalProgressBar);
90 private static JPanel createHorizontalProgressBar()
92 JPanel panel = new JPanel();
93 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
95 // Plain progress bar.
96 final JProgressBar hor1 = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
98 final JSlider slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
99 slider1.addChangeListener(new ChangeListener()
101 public void stateChanged(ChangeEvent event)
103 hor1.setValue(slider1.getValue());
108 panel.add(Box.createVerticalStrut(5));
110 // Plain progress bar with some text.
111 final JProgressBar hor2 = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
112 hor2.setString("ProgressBar Demo");
113 hor2.setStringPainted(true);
115 final JSlider slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
116 slider2.addChangeListener(new ChangeListener()
118 public void stateChanged(ChangeEvent event)
120 hor2.setValue(slider2.getValue());
125 panel.add(Box.createVerticalStrut(5));
127 // Indeterminate progress bar.
128 final JProgressBar hor3 = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
129 hor3.setIndeterminate(true);
132 panel.add(Box.createVerticalStrut(5));
134 // Indeterminate progress bar with text.
135 final JProgressBar hor4 = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
136 hor4.setIndeterminate(true);
137 hor4.setString("Indeterminate ProgressBar");
138 hor4.setStringPainted(true);
144 private static JPanel createVerticalProgressBar()
146 JPanel panel = new JPanel();
147 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
148 final JProgressBar vert = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
150 final JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 0);
151 slider.addChangeListener(new ChangeListener()
153 public void stateChanged(ChangeEvent event)
155 vert.setValue(slider.getValue());
160 panel.add(Box.createHorizontalStrut(5));
162 final JProgressBar vert2 = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
163 vert2.setString("ProgressBar Demo");
165 vert2.setStringPainted(true);
166 final JSlider slider2 = new JSlider(JSlider.VERTICAL, 0, 100, 0);
167 slider2.addChangeListener(new ChangeListener()
169 public void stateChanged(ChangeEvent event)
171 vert2.setValue(slider2.getValue());
176 panel.add(Box.createHorizontalStrut(5));
178 // Indeterminate progress bar.
179 final JProgressBar vert3 = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
180 vert3.setIndeterminate(true);
183 panel.add(Box.createHorizontalStrut(5));
185 // Indeterminate progress bar with text.
186 final JProgressBar vert4 = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
187 vert4.setIndeterminate(true);
188 vert4.setString("Indeterminate ProgressBar");
189 vert4.setStringPainted(true);
194 public void actionPerformed(ActionEvent event)
196 if (event.getActionCommand().equals("CLOSE"))
203 * The entry point when running as a standalone program.
205 * @param args command line arguments
207 public static void main(String[] args)
209 SwingUtilities.invokeLater(
214 ProgressBarDemo app = new ProgressBarDemo();
215 app.initFrameContent();
216 JFrame frame = new JFrame("ProgressBar Demo");
217 frame.getContentPane().add(app);
219 frame.setVisible(true);
225 * Returns a DemoFactory that creates a ProgressBarDemo.
227 * @return a DemoFactory that creates a ProgressBarDemo
229 public static DemoFactory createDemoFactory()
231 return new DemoFactory()
233 public JComponent createDemo()
235 return new ProgressBarDemo();