1 |
781 |
jeremybenn |
/* ButtonDemo.java -- An example showing various buttons in Swing.
|
2 |
|
|
Copyright (C) 2005, 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.GridLayout;
|
27 |
|
|
import java.awt.event.ActionEvent;
|
28 |
|
|
import java.awt.event.ActionListener;
|
29 |
|
|
|
30 |
|
|
import javax.swing.BorderFactory;
|
31 |
|
|
import javax.swing.ButtonGroup;
|
32 |
|
|
import javax.swing.JButton;
|
33 |
|
|
import javax.swing.JCheckBox;
|
34 |
|
|
import javax.swing.JComponent;
|
35 |
|
|
import javax.swing.JFrame;
|
36 |
|
|
import javax.swing.JPanel;
|
37 |
|
|
import javax.swing.JRadioButton;
|
38 |
|
|
import javax.swing.JToggleButton;
|
39 |
|
|
import javax.swing.SwingConstants;
|
40 |
|
|
import javax.swing.SwingUtilities;
|
41 |
|
|
import javax.swing.plaf.metal.MetalIconFactory;
|
42 |
|
|
|
43 |
|
|
/**
|
44 |
|
|
* A simple button demo showing various buttons in different states.
|
45 |
|
|
*/
|
46 |
|
|
public class ButtonDemo
|
47 |
|
|
extends JPanel
|
48 |
|
|
implements ActionListener
|
49 |
|
|
{
|
50 |
|
|
|
51 |
|
|
private JCheckBox buttonState;
|
52 |
|
|
private JButton button1;
|
53 |
|
|
private JButton button2;
|
54 |
|
|
private JButton button3;
|
55 |
|
|
private JButton button4;
|
56 |
|
|
|
57 |
|
|
private JCheckBox toggleState;
|
58 |
|
|
private JToggleButton toggle1;
|
59 |
|
|
private JToggleButton toggle2;
|
60 |
|
|
private JToggleButton toggle3;
|
61 |
|
|
private JToggleButton toggle4;
|
62 |
|
|
|
63 |
|
|
private JCheckBox checkBoxState;
|
64 |
|
|
private JCheckBox checkBox1;
|
65 |
|
|
private JCheckBox checkBox2;
|
66 |
|
|
private JCheckBox checkBox3;
|
67 |
|
|
|
68 |
|
|
private JCheckBox radioState;
|
69 |
|
|
private JRadioButton radio1;
|
70 |
|
|
private JRadioButton radio2;
|
71 |
|
|
private JRadioButton radio3;
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* Creates a new demo instance.
|
75 |
|
|
*/
|
76 |
|
|
public ButtonDemo()
|
77 |
|
|
{
|
78 |
|
|
createContent();
|
79 |
|
|
// initFrameContent() is only called (from main) when running this app
|
80 |
|
|
// standalone
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* When the demo is run independently, the frame is displayed, so we should
|
85 |
|
|
* initialise the content panel (including the demo content and a close
|
86 |
|
|
* button). But when the demo is run as part of the Swing activity board,
|
87 |
|
|
* only the demo content panel is used, the frame itself is never displayed,
|
88 |
|
|
* so we can avoid this step.
|
89 |
|
|
*/
|
90 |
|
|
void initFrameContent()
|
91 |
|
|
{
|
92 |
|
|
JPanel closePanel = new JPanel();
|
93 |
|
|
JButton closeButton = new JButton("Close");
|
94 |
|
|
closeButton.setActionCommand("CLOSE");
|
95 |
|
|
closeButton.addActionListener(this);
|
96 |
|
|
closePanel.add(closeButton);
|
97 |
|
|
add(closePanel, BorderLayout.SOUTH);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* Returns a panel with the demo content. The panel
|
102 |
|
|
* uses a BorderLayout(), and the BorderLayout.SOUTH area
|
103 |
|
|
* is empty, to allow callers to add controls to the
|
104 |
|
|
* bottom of the panel if they want to (a close button is
|
105 |
|
|
* added if this demo is being run as a standalone demo).
|
106 |
|
|
*/
|
107 |
|
|
private void createContent()
|
108 |
|
|
{
|
109 |
|
|
setLayout(new BorderLayout());
|
110 |
|
|
JPanel panel = new JPanel(new GridLayout(4, 1));
|
111 |
|
|
panel.add(createButtonPanel());
|
112 |
|
|
panel.add(createTogglePanel());
|
113 |
|
|
panel.add(createCheckBoxPanel());
|
114 |
|
|
panel.add(createRadioPanel());
|
115 |
|
|
add(panel);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
private JPanel createButtonPanel()
|
119 |
|
|
{
|
120 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
121 |
|
|
this.buttonState = new JCheckBox("Enabled", true);
|
122 |
|
|
this.buttonState.setActionCommand("BUTTON_STATE");
|
123 |
|
|
this.buttonState.addActionListener(this);
|
124 |
|
|
panel.add(this.buttonState, BorderLayout.EAST);
|
125 |
|
|
|
126 |
|
|
JPanel buttonPanel = new JPanel();
|
127 |
|
|
buttonPanel.setBorder(BorderFactory.createTitledBorder("JButton"));
|
128 |
|
|
this.button1 = new JButton("Button 1");
|
129 |
|
|
|
130 |
|
|
this.button2 = new JButton("Button 2");
|
131 |
|
|
this.button2.setIcon(MetalIconFactory.getInternalFrameDefaultMenuIcon());
|
132 |
|
|
|
133 |
|
|
this.button3 = new JButton("Button 3");
|
134 |
|
|
this.button3.setIcon(MetalIconFactory.getFileChooserHomeFolderIcon());
|
135 |
|
|
this.button3.setHorizontalTextPosition(SwingConstants.CENTER);
|
136 |
|
|
this.button3.setVerticalTextPosition(SwingConstants.BOTTOM);
|
137 |
|
|
|
138 |
|
|
this.button4 = new JButton("Button 4");
|
139 |
|
|
this.button4.setIcon(MetalIconFactory.getFileChooserUpFolderIcon());
|
140 |
|
|
this.button4.setText(null);
|
141 |
|
|
|
142 |
|
|
buttonPanel.add(button1);
|
143 |
|
|
buttonPanel.add(button2);
|
144 |
|
|
buttonPanel.add(button3);
|
145 |
|
|
buttonPanel.add(button4);
|
146 |
|
|
|
147 |
|
|
panel.add(buttonPanel);
|
148 |
|
|
|
149 |
|
|
return panel;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
private JPanel createTogglePanel()
|
153 |
|
|
{
|
154 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
155 |
|
|
|
156 |
|
|
this.toggleState = new JCheckBox("Enabled", true);
|
157 |
|
|
this.toggleState.setActionCommand("TOGGLE_STATE");
|
158 |
|
|
this.toggleState.addActionListener(this);
|
159 |
|
|
|
160 |
|
|
panel.add(this.toggleState, BorderLayout.EAST);
|
161 |
|
|
|
162 |
|
|
JPanel buttonPanel = new JPanel();
|
163 |
|
|
buttonPanel.setBorder(BorderFactory.createTitledBorder("JToggleButton"));
|
164 |
|
|
|
165 |
|
|
this.toggle1 = new JToggleButton("Toggle 1");
|
166 |
|
|
|
167 |
|
|
this.toggle2 = new JToggleButton("Toggle 2");
|
168 |
|
|
this.toggle2.setIcon(MetalIconFactory.getInternalFrameDefaultMenuIcon());
|
169 |
|
|
|
170 |
|
|
this.toggle3 = new JToggleButton("Toggle 3");
|
171 |
|
|
this.toggle3.setIcon(MetalIconFactory.getFileChooserHomeFolderIcon());
|
172 |
|
|
this.toggle3.setHorizontalTextPosition(SwingConstants.CENTER);
|
173 |
|
|
this.toggle3.setVerticalTextPosition(SwingConstants.BOTTOM);
|
174 |
|
|
|
175 |
|
|
this.toggle4 = new JToggleButton("Toggle 4");
|
176 |
|
|
this.toggle4.setIcon(MetalIconFactory.getFileChooserUpFolderIcon());
|
177 |
|
|
this.toggle4.setText(null);
|
178 |
|
|
|
179 |
|
|
ButtonGroup toggleGroup = new ButtonGroup();
|
180 |
|
|
toggleGroup.add(toggle1);
|
181 |
|
|
toggleGroup.add(toggle2);
|
182 |
|
|
toggleGroup.add(toggle3);
|
183 |
|
|
toggleGroup.add(toggle4);
|
184 |
|
|
|
185 |
|
|
buttonPanel.add(toggle1);
|
186 |
|
|
buttonPanel.add(toggle2);
|
187 |
|
|
buttonPanel.add(toggle3);
|
188 |
|
|
buttonPanel.add(toggle4);
|
189 |
|
|
|
190 |
|
|
panel.add(buttonPanel);
|
191 |
|
|
|
192 |
|
|
return panel;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
private JPanel createCheckBoxPanel()
|
196 |
|
|
{
|
197 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
198 |
|
|
|
199 |
|
|
this.checkBoxState = new JCheckBox("Enabled", true);
|
200 |
|
|
this.checkBoxState.setActionCommand("CHECKBOX_STATE");
|
201 |
|
|
this.checkBoxState.addActionListener(this);
|
202 |
|
|
|
203 |
|
|
panel.add(this.checkBoxState, BorderLayout.EAST);
|
204 |
|
|
|
205 |
|
|
JPanel buttonPanel = new JPanel();
|
206 |
|
|
buttonPanel.setBorder(BorderFactory.createTitledBorder("JCheckBox"));
|
207 |
|
|
this.checkBox1 = new JCheckBox("CheckBox 1");
|
208 |
|
|
|
209 |
|
|
this.checkBox2 = new JCheckBox("CheckBox 2");
|
210 |
|
|
|
211 |
|
|
this.checkBox3 = new JCheckBox("CheckBox 3");
|
212 |
|
|
|
213 |
|
|
buttonPanel.add(checkBox1);
|
214 |
|
|
buttonPanel.add(checkBox2);
|
215 |
|
|
buttonPanel.add(checkBox3);
|
216 |
|
|
|
217 |
|
|
panel.add(buttonPanel);
|
218 |
|
|
|
219 |
|
|
return panel;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
private JPanel createRadioPanel()
|
223 |
|
|
{
|
224 |
|
|
JPanel panel = new JPanel(new BorderLayout());
|
225 |
|
|
|
226 |
|
|
this.radioState = new JCheckBox("Enabled", true);
|
227 |
|
|
this.radioState.setActionCommand("RADIO_STATE");
|
228 |
|
|
this.radioState.addActionListener(this);
|
229 |
|
|
panel.add(this.radioState, BorderLayout.EAST);
|
230 |
|
|
|
231 |
|
|
JPanel buttonPanel = new JPanel();
|
232 |
|
|
buttonPanel.setBorder(BorderFactory.createTitledBorder("JRadioButton"));
|
233 |
|
|
this.radio1 = new JRadioButton("Radio 1");
|
234 |
|
|
|
235 |
|
|
this.radio2 = new JRadioButton("Radio 2");
|
236 |
|
|
|
237 |
|
|
this.radio3 = new JRadioButton("Radio 3");
|
238 |
|
|
|
239 |
|
|
ButtonGroup radioGroup = new ButtonGroup();
|
240 |
|
|
radioGroup.add(radio1);
|
241 |
|
|
radioGroup.add(radio2);
|
242 |
|
|
radioGroup.add(radio3);
|
243 |
|
|
|
244 |
|
|
buttonPanel.add(radio1);
|
245 |
|
|
buttonPanel.add(radio2);
|
246 |
|
|
buttonPanel.add(radio3);
|
247 |
|
|
|
248 |
|
|
panel.add(buttonPanel);
|
249 |
|
|
|
250 |
|
|
return panel;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
public void actionPerformed(ActionEvent e)
|
254 |
|
|
{
|
255 |
|
|
if (e.getActionCommand().equals("BUTTON_STATE"))
|
256 |
|
|
{
|
257 |
|
|
button1.setEnabled(buttonState.isSelected());
|
258 |
|
|
button2.setEnabled(buttonState.isSelected());
|
259 |
|
|
button3.setEnabled(buttonState.isSelected());
|
260 |
|
|
button4.setEnabled(buttonState.isSelected());
|
261 |
|
|
}
|
262 |
|
|
else if (e.getActionCommand().equals("TOGGLE_STATE"))
|
263 |
|
|
{
|
264 |
|
|
toggle1.setEnabled(toggleState.isSelected());
|
265 |
|
|
toggle2.setEnabled(toggleState.isSelected());
|
266 |
|
|
toggle3.setEnabled(toggleState.isSelected());
|
267 |
|
|
toggle4.setEnabled(toggleState.isSelected());
|
268 |
|
|
}
|
269 |
|
|
else if (e.getActionCommand().equals("CHECKBOX_STATE"))
|
270 |
|
|
{
|
271 |
|
|
checkBox1.setEnabled(checkBoxState.isSelected());
|
272 |
|
|
checkBox2.setEnabled(checkBoxState.isSelected());
|
273 |
|
|
checkBox3.setEnabled(checkBoxState.isSelected());
|
274 |
|
|
}
|
275 |
|
|
else if (e.getActionCommand().equals("RADIO_STATE"))
|
276 |
|
|
{
|
277 |
|
|
radio1.setEnabled(radioState.isSelected());
|
278 |
|
|
radio2.setEnabled(radioState.isSelected());
|
279 |
|
|
radio3.setEnabled(radioState.isSelected());
|
280 |
|
|
}
|
281 |
|
|
else if (e.getActionCommand().equals("CLOSE"))
|
282 |
|
|
{
|
283 |
|
|
System.exit(0);
|
284 |
|
|
}
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
public static void main(String[] args)
|
288 |
|
|
{
|
289 |
|
|
SwingUtilities.invokeLater
|
290 |
|
|
(new Runnable()
|
291 |
|
|
{
|
292 |
|
|
public void run()
|
293 |
|
|
{
|
294 |
|
|
ButtonDemo app = new ButtonDemo();
|
295 |
|
|
app.initFrameContent();
|
296 |
|
|
JFrame frame = new JFrame("ButtonDemo");
|
297 |
|
|
frame.getContentPane().add(app);
|
298 |
|
|
frame.pack();
|
299 |
|
|
frame.setVisible(true);
|
300 |
|
|
}
|
301 |
|
|
});
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/**
|
305 |
|
|
* Returns a DemoFactory that creates a ButtonDemo.
|
306 |
|
|
*
|
307 |
|
|
* @return a DemoFactory that creates a ButtonDemo
|
308 |
|
|
*/
|
309 |
|
|
public static DemoFactory createDemoFactory()
|
310 |
|
|
{
|
311 |
|
|
return new DemoFactory()
|
312 |
|
|
{
|
313 |
|
|
public JComponent createDemo()
|
314 |
|
|
{
|
315 |
|
|
return new ButtonDemo();
|
316 |
|
|
}
|
317 |
|
|
};
|
318 |
|
|
}
|
319 |
|
|
}
|