1 |
781 |
jeremybenn |
/* ProgressBarDemo.java -- A demonstration of JProgressBars
|
2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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)
|
9 |
|
|
any later version.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
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
|
24 |
|
|
combination.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.classpath.examples.swing;
|
40 |
|
|
|
41 |
|
|
import java.awt.event.ActionEvent;
|
42 |
|
|
import java.awt.event.ActionListener;
|
43 |
|
|
|
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;
|
55 |
|
|
|
56 |
|
|
public class ProgressBarDemo
|
57 |
|
|
extends JPanel
|
58 |
|
|
implements ActionListener
|
59 |
|
|
{
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Creates a new ProgressBarDemo window with the specified title.
|
63 |
|
|
*/
|
64 |
|
|
ProgressBarDemo()
|
65 |
|
|
{
|
66 |
|
|
super();
|
67 |
|
|
createContent();
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
void initFrameContent()
|
71 |
|
|
{
|
72 |
|
|
JPanel closePanel = new JPanel();
|
73 |
|
|
JButton closeButton = new JButton("Close");
|
74 |
|
|
closeButton.setActionCommand("CLOSE");
|
75 |
|
|
closeButton.addActionListener(this);
|
76 |
|
|
closePanel.add(closeButton);
|
77 |
|
|
add(closePanel);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
private void createContent()
|
81 |
|
|
{
|
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);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
private static JPanel createHorizontalProgressBar()
|
91 |
|
|
{
|
92 |
|
|
JPanel panel = new JPanel();
|
93 |
|
|
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
94 |
|
|
|
95 |
|
|
// Plain progress bar.
|
96 |
|
|
final JProgressBar hor1 = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
|
97 |
|
|
panel.add(hor1);
|
98 |
|
|
final JSlider slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
|
99 |
|
|
slider1.addChangeListener(new ChangeListener()
|
100 |
|
|
{
|
101 |
|
|
public void stateChanged(ChangeEvent event)
|
102 |
|
|
{
|
103 |
|
|
hor1.setValue(slider1.getValue());
|
104 |
|
|
}
|
105 |
|
|
});
|
106 |
|
|
panel.add(slider1);
|
107 |
|
|
|
108 |
|
|
panel.add(Box.createVerticalStrut(5));
|
109 |
|
|
|
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);
|
114 |
|
|
panel.add(hor2);
|
115 |
|
|
final JSlider slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
|
116 |
|
|
slider2.addChangeListener(new ChangeListener()
|
117 |
|
|
{
|
118 |
|
|
public void stateChanged(ChangeEvent event)
|
119 |
|
|
{
|
120 |
|
|
hor2.setValue(slider2.getValue());
|
121 |
|
|
}
|
122 |
|
|
});
|
123 |
|
|
panel.add(slider2);
|
124 |
|
|
|
125 |
|
|
panel.add(Box.createVerticalStrut(5));
|
126 |
|
|
|
127 |
|
|
// Indeterminate progress bar.
|
128 |
|
|
final JProgressBar hor3 = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
|
129 |
|
|
hor3.setIndeterminate(true);
|
130 |
|
|
panel.add(hor3);
|
131 |
|
|
|
132 |
|
|
panel.add(Box.createVerticalStrut(5));
|
133 |
|
|
|
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);
|
139 |
|
|
panel.add(hor4);
|
140 |
|
|
|
141 |
|
|
return panel;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
private static JPanel createVerticalProgressBar()
|
145 |
|
|
{
|
146 |
|
|
JPanel panel = new JPanel();
|
147 |
|
|
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
|
148 |
|
|
final JProgressBar vert = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
|
149 |
|
|
panel.add(vert);
|
150 |
|
|
final JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 0);
|
151 |
|
|
slider.addChangeListener(new ChangeListener()
|
152 |
|
|
{
|
153 |
|
|
public void stateChanged(ChangeEvent event)
|
154 |
|
|
{
|
155 |
|
|
vert.setValue(slider.getValue());
|
156 |
|
|
}
|
157 |
|
|
});
|
158 |
|
|
panel.add(slider);
|
159 |
|
|
|
160 |
|
|
panel.add(Box.createHorizontalStrut(5));
|
161 |
|
|
|
162 |
|
|
final JProgressBar vert2 = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
|
163 |
|
|
vert2.setString("ProgressBar Demo");
|
164 |
|
|
panel.add(vert2);
|
165 |
|
|
vert2.setStringPainted(true);
|
166 |
|
|
final JSlider slider2 = new JSlider(JSlider.VERTICAL, 0, 100, 0);
|
167 |
|
|
slider2.addChangeListener(new ChangeListener()
|
168 |
|
|
{
|
169 |
|
|
public void stateChanged(ChangeEvent event)
|
170 |
|
|
{
|
171 |
|
|
vert2.setValue(slider2.getValue());
|
172 |
|
|
}
|
173 |
|
|
});
|
174 |
|
|
panel.add(slider2);
|
175 |
|
|
|
176 |
|
|
panel.add(Box.createHorizontalStrut(5));
|
177 |
|
|
|
178 |
|
|
// Indeterminate progress bar.
|
179 |
|
|
final JProgressBar vert3 = new JProgressBar(JProgressBar.VERTICAL, 0, 100);
|
180 |
|
|
vert3.setIndeterminate(true);
|
181 |
|
|
panel.add(vert3);
|
182 |
|
|
|
183 |
|
|
panel.add(Box.createHorizontalStrut(5));
|
184 |
|
|
|
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);
|
190 |
|
|
panel.add(vert4);
|
191 |
|
|
return panel;
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
public void actionPerformed(ActionEvent event)
|
195 |
|
|
{
|
196 |
|
|
if (event.getActionCommand().equals("CLOSE"))
|
197 |
|
|
{
|
198 |
|
|
System.exit(0);
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/**
|
203 |
|
|
* The entry point when running as a standalone program.
|
204 |
|
|
*
|
205 |
|
|
* @param args command line arguments
|
206 |
|
|
*/
|
207 |
|
|
public static void main(String[] args)
|
208 |
|
|
{
|
209 |
|
|
SwingUtilities.invokeLater(
|
210 |
|
|
new Runnable()
|
211 |
|
|
{
|
212 |
|
|
public void run()
|
213 |
|
|
{
|
214 |
|
|
ProgressBarDemo app = new ProgressBarDemo();
|
215 |
|
|
app.initFrameContent();
|
216 |
|
|
JFrame frame = new JFrame("ProgressBar Demo");
|
217 |
|
|
frame.getContentPane().add(app);
|
218 |
|
|
frame.pack();
|
219 |
|
|
frame.setVisible(true);
|
220 |
|
|
}
|
221 |
|
|
});
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
/**
|
225 |
|
|
* Returns a DemoFactory that creates a ProgressBarDemo.
|
226 |
|
|
*
|
227 |
|
|
* @return a DemoFactory that creates a ProgressBarDemo
|
228 |
|
|
*/
|
229 |
|
|
public static DemoFactory createDemoFactory()
|
230 |
|
|
{
|
231 |
|
|
return new DemoFactory()
|
232 |
|
|
{
|
233 |
|
|
public JComponent createDemo()
|
234 |
|
|
{
|
235 |
|
|
return new ProgressBarDemo();
|
236 |
|
|
}
|
237 |
|
|
};
|
238 |
|
|
}
|
239 |
|
|
}
|