1 |
781 |
jeremybenn |
/* SpinnerDemo.java -- An example showing various spinners in Swing.
|
2 |
|
|
Copyright (C) 2006, Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath examples.
|
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 |
|
|
|
22 |
|
|
|
23 |
|
|
package gnu.classpath.examples.swing;
|
24 |
|
|
|
25 |
|
|
import java.awt.BorderLayout;
|
26 |
|
|
import java.awt.Font;
|
27 |
|
|
import java.awt.GridLayout;
|
28 |
|
|
import java.awt.event.ActionEvent;
|
29 |
|
|
import java.awt.event.ActionListener;
|
30 |
|
|
import java.util.Calendar;
|
31 |
|
|
import java.util.Date;
|
32 |
|
|
|
33 |
|
|
import javax.swing.BorderFactory;
|
34 |
|
|
import javax.swing.JButton;
|
35 |
|
|
import javax.swing.JCheckBox;
|
36 |
|
|
import javax.swing.JComponent;
|
37 |
|
|
import javax.swing.JFrame;
|
38 |
|
|
import javax.swing.JPanel;
|
39 |
|
|
import javax.swing.JSpinner;
|
40 |
|
|
import javax.swing.SpinnerDateModel;
|
41 |
|
|
import javax.swing.SpinnerListModel;
|
42 |
|
|
import javax.swing.SpinnerNumberModel;
|
43 |
|
|
import javax.swing.SwingUtilities;
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* A simple demo showing various spinners in different states.
|
47 |
|
|
*/
|
48 |
|
|
public class SpinnerDemo
|
49 |
|
|
extends JPanel
|
50 |
|
|
implements ActionListener
|
51 |
|
|
{
|
52 |
|
|
private JCheckBox spinnerState1;
|
53 |
|
|
private JSpinner spinner1;
|
54 |
|
|
private JSpinner spinner2;
|
55 |
|
|
|
56 |
|
|
private JCheckBox spinnerState2;
|
57 |
|
|
private JSpinner spinner3;
|
58 |
|
|
private JSpinner spinner4;
|
59 |
|
|
|
60 |
|
|
private JCheckBox spinnerState3;
|
61 |
|
|
private JSpinner spinner5;
|
62 |
|
|
private JSpinner spinner6;
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* Creates a new demo instance.
|
66 |
|
|
*/
|
67 |
|
|
public SpinnerDemo()
|
68 |
|
|
{
|
69 |
|
|
super();
|
70 |
|
|
createContent();
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* When the demo is run independently, the frame is displayed, so we should
|
75 |
|
|
* initialise the content panel (including the demo content and a close
|
76 |
|
|
* button). But when the demo is run as part of the Swing activity board,
|
77 |
|
|
* only the demo content panel is used, the frame itself is never displayed,
|
78 |
|
|
* so we can avoid this step.
|
79 |
|
|
*/
|
80 |
|
|
void initFrameContent()
|
81 |
|
|
{
|
82 |
|
|
JPanel closePanel = new JPanel();
|
83 |
|
|
JButton closeButton = new JButton("Close");
|
84 |
|
|
closeButton.setActionCommand("CLOSE");
|
85 |
|
|
closeButton.addActionListener(this);
|
86 |
|
|
closePanel.add(closeButton);
|
87 |
|
|
add(closePanel, BorderLayout.SOUTH);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* Returns a panel with the demo content. The panel
|
92 |
|
|
* uses a BorderLayout(), and the BorderLayout.SOUTH area
|
93 |
|
|
* is empty, to allow callers to add controls to the
|
94 |
|
|
* bottom of the panel if they want to (a close button is
|
95 |
|
|
* added if this demo is being run as a standalone demo).
|
96 |
|
|
*/
|
97 |
|
|
private void createContent()
|
98 |
|
|
{
|
99 |
|
|
setLayout(new BorderLayout());
|
100 |
|
|
JPanel panel = new JPanel(new GridLayout(3, 1));
|
101 |
|
|
panel.add(createPanel1());
|
102 |
|
|
panel.add(createPanel2());
|
103 |
|
|
panel.add(createPanel3());
|
104 |
|
|
add(panel);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
private JPanel createPanel1()
|
108 |
|
|
{
|
109 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
110 |
|
|
this.spinnerState1 = new JCheckBox("Enabled", true);
|
111 |
|
|
this.spinnerState1.setActionCommand("COMBO_STATE1");
|
112 |
|
|
this.spinnerState1.addActionListener(this);
|
113 |
|
|
panel.add(this.spinnerState1, BorderLayout.EAST);
|
114 |
|
|
|
115 |
|
|
JPanel controlPanel = new JPanel();
|
116 |
|
|
controlPanel.setBorder(BorderFactory.createTitledBorder(
|
117 |
|
|
"Number Spinner: "));
|
118 |
|
|
this.spinner1 = new JSpinner(new SpinnerNumberModel(5.0, 0.0, 10.0, 0.5));
|
119 |
|
|
this.spinner2 = new JSpinner(new SpinnerNumberModel(50, 0, 100, 5));
|
120 |
|
|
this.spinner2.setFont(new Font("Dialog", Font.PLAIN, 20));
|
121 |
|
|
controlPanel.add(this.spinner1);
|
122 |
|
|
controlPanel.add(this.spinner2);
|
123 |
|
|
|
124 |
|
|
panel.add(controlPanel);
|
125 |
|
|
|
126 |
|
|
return panel;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
private JPanel createPanel2()
|
130 |
|
|
{
|
131 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
132 |
|
|
this.spinnerState2 = new JCheckBox("Enabled", true);
|
133 |
|
|
this.spinnerState2.setActionCommand("COMBO_STATE2");
|
134 |
|
|
this.spinnerState2.addActionListener(this);
|
135 |
|
|
panel.add(this.spinnerState2, BorderLayout.EAST);
|
136 |
|
|
|
137 |
|
|
JPanel controlPanel = new JPanel();
|
138 |
|
|
controlPanel.setBorder(BorderFactory.createTitledBorder("Date Spinner: "));
|
139 |
|
|
this.spinner3 = new JSpinner(new SpinnerDateModel(new Date(), null, null,
|
140 |
|
|
Calendar.DATE));
|
141 |
|
|
|
142 |
|
|
this.spinner4 = new JSpinner(new SpinnerDateModel(new Date(), null, null,
|
143 |
|
|
Calendar.YEAR));
|
144 |
|
|
this.spinner4.setFont(new Font("Dialog", Font.PLAIN, 20));
|
145 |
|
|
|
146 |
|
|
controlPanel.add(this.spinner3);
|
147 |
|
|
controlPanel.add(this.spinner4);
|
148 |
|
|
|
149 |
|
|
panel.add(controlPanel);
|
150 |
|
|
|
151 |
|
|
return panel;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
private JPanel createPanel3()
|
155 |
|
|
{
|
156 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
157 |
|
|
this.spinnerState3 = new JCheckBox("Enabled", true);
|
158 |
|
|
this.spinnerState3.setActionCommand("COMBO_STATE3");
|
159 |
|
|
this.spinnerState3.addActionListener(this);
|
160 |
|
|
panel.add(this.spinnerState3, BorderLayout.EAST);
|
161 |
|
|
|
162 |
|
|
JPanel controlPanel = new JPanel();
|
163 |
|
|
controlPanel.setBorder(BorderFactory.createTitledBorder("List Spinner: "));
|
164 |
|
|
this.spinner5 = new JSpinner(new SpinnerListModel(new Object[] {"Red",
|
165 |
|
|
"Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}));
|
166 |
|
|
|
167 |
|
|
this.spinner6 = new JSpinner(new SpinnerListModel(new Object[] {"Red",
|
168 |
|
|
"Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}));
|
169 |
|
|
this.spinner6.setValue("Yellow");
|
170 |
|
|
this.spinner6.setFont(new Font("Dialog", Font.PLAIN, 20));
|
171 |
|
|
|
172 |
|
|
controlPanel.add(this.spinner5);
|
173 |
|
|
controlPanel.add(this.spinner6);
|
174 |
|
|
|
175 |
|
|
panel.add(controlPanel);
|
176 |
|
|
|
177 |
|
|
return panel;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
public void actionPerformed(ActionEvent e)
|
181 |
|
|
{
|
182 |
|
|
if (e.getActionCommand().equals("COMBO_STATE1"))
|
183 |
|
|
{
|
184 |
|
|
spinner1.setEnabled(spinnerState1.isSelected());
|
185 |
|
|
spinner2.setEnabled(spinnerState1.isSelected());
|
186 |
|
|
}
|
187 |
|
|
else if (e.getActionCommand().equals("COMBO_STATE2"))
|
188 |
|
|
{
|
189 |
|
|
spinner3.setEnabled(spinnerState2.isSelected());
|
190 |
|
|
spinner4.setEnabled(spinnerState2.isSelected());
|
191 |
|
|
}
|
192 |
|
|
else if (e.getActionCommand().equals("COMBO_STATE3"))
|
193 |
|
|
{
|
194 |
|
|
spinner5.setEnabled(spinnerState3.isSelected());
|
195 |
|
|
spinner6.setEnabled(spinnerState3.isSelected());
|
196 |
|
|
}
|
197 |
|
|
else if (e.getActionCommand().equals("CLOSE"))
|
198 |
|
|
{
|
199 |
|
|
System.exit(0);
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
public static void main(String[] args)
|
204 |
|
|
{
|
205 |
|
|
SwingUtilities.invokeLater
|
206 |
|
|
(new Runnable()
|
207 |
|
|
{
|
208 |
|
|
public void run()
|
209 |
|
|
{
|
210 |
|
|
SpinnerDemo app = new SpinnerDemo();
|
211 |
|
|
app.initFrameContent();
|
212 |
|
|
JFrame frame = new JFrame("Spinner Demo");
|
213 |
|
|
frame.getContentPane().add(app);
|
214 |
|
|
frame.pack();
|
215 |
|
|
frame.setVisible(true);
|
216 |
|
|
}
|
217 |
|
|
});
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/**
|
221 |
|
|
* Returns a DemoFactory that creates a SpinnerDemo.
|
222 |
|
|
*
|
223 |
|
|
* @return a DemoFactory that creates a SpinnerDemo
|
224 |
|
|
*/
|
225 |
|
|
public static DemoFactory createDemoFactory()
|
226 |
|
|
{
|
227 |
|
|
return new DemoFactory()
|
228 |
|
|
{
|
229 |
|
|
public JComponent createDemo()
|
230 |
|
|
{
|
231 |
|
|
return new SpinnerDemo();
|
232 |
|
|
}
|
233 |
|
|
};
|
234 |
|
|
}
|
235 |
|
|
}
|