1 /* ButtonDemo.java -- An example showing various buttons in Swing.
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
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
23 package gnu
.classpath
.examples
.swing
;
25 import java
.awt
.BorderLayout
;
26 import java
.awt
.GridLayout
;
27 import java
.awt
.event
.ActionEvent
;
28 import java
.awt
.event
.ActionListener
;
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
;
44 * A simple button demo showing various buttons in different states.
46 public class ButtonDemo
48 implements ActionListener
51 private JCheckBox buttonState
;
52 private JButton button1
;
53 private JButton button2
;
54 private JButton button3
;
55 private JButton button4
;
57 private JCheckBox toggleState
;
58 private JToggleButton toggle1
;
59 private JToggleButton toggle2
;
60 private JToggleButton toggle3
;
61 private JToggleButton toggle4
;
63 private JCheckBox checkBoxState
;
64 private JCheckBox checkBox1
;
65 private JCheckBox checkBox2
;
66 private JCheckBox checkBox3
;
68 private JCheckBox radioState
;
69 private JRadioButton radio1
;
70 private JRadioButton radio2
;
71 private JRadioButton radio3
;
74 * Creates a new demo instance.
79 // initFrameContent() is only called (from main) when running this app
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.
90 void initFrameContent()
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
);
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).
107 private void createContent()
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());
118 private JPanel
createButtonPanel()
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
);
126 JPanel buttonPanel
= new JPanel();
127 buttonPanel
.setBorder(BorderFactory
.createTitledBorder("JButton"));
128 this.button1
= new JButton("Button 1");
130 this.button2
= new JButton("Button 2");
131 this.button2
.setIcon(MetalIconFactory
.getInternalFrameDefaultMenuIcon());
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
);
138 this.button4
= new JButton("Button 4");
139 this.button4
.setIcon(MetalIconFactory
.getFileChooserUpFolderIcon());
140 this.button4
.setText(null);
142 buttonPanel
.add(button1
);
143 buttonPanel
.add(button2
);
144 buttonPanel
.add(button3
);
145 buttonPanel
.add(button4
);
147 panel
.add(buttonPanel
);
152 private JPanel
createTogglePanel()
154 JPanel panel
= new JPanel(new BorderLayout());
156 this.toggleState
= new JCheckBox("Enabled", true);
157 this.toggleState
.setActionCommand("TOGGLE_STATE");
158 this.toggleState
.addActionListener(this);
160 panel
.add(this.toggleState
, BorderLayout
.EAST
);
162 JPanel buttonPanel
= new JPanel();
163 buttonPanel
.setBorder(BorderFactory
.createTitledBorder("JToggleButton"));
165 this.toggle1
= new JToggleButton("Toggle 1");
167 this.toggle2
= new JToggleButton("Toggle 2");
168 this.toggle2
.setIcon(MetalIconFactory
.getInternalFrameDefaultMenuIcon());
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
);
175 this.toggle4
= new JToggleButton("Toggle 4");
176 this.toggle4
.setIcon(MetalIconFactory
.getFileChooserUpFolderIcon());
177 this.toggle4
.setText(null);
179 ButtonGroup toggleGroup
= new ButtonGroup();
180 toggleGroup
.add(toggle1
);
181 toggleGroup
.add(toggle2
);
182 toggleGroup
.add(toggle3
);
183 toggleGroup
.add(toggle4
);
185 buttonPanel
.add(toggle1
);
186 buttonPanel
.add(toggle2
);
187 buttonPanel
.add(toggle3
);
188 buttonPanel
.add(toggle4
);
190 panel
.add(buttonPanel
);
195 private JPanel
createCheckBoxPanel()
197 JPanel panel
= new JPanel(new BorderLayout());
199 this.checkBoxState
= new JCheckBox("Enabled", true);
200 this.checkBoxState
.setActionCommand("CHECKBOX_STATE");
201 this.checkBoxState
.addActionListener(this);
203 panel
.add(this.checkBoxState
, BorderLayout
.EAST
);
205 JPanel buttonPanel
= new JPanel();
206 buttonPanel
.setBorder(BorderFactory
.createTitledBorder("JCheckBox"));
207 this.checkBox1
= new JCheckBox("CheckBox 1");
209 this.checkBox2
= new JCheckBox("CheckBox 2");
211 this.checkBox3
= new JCheckBox("CheckBox 3");
213 buttonPanel
.add(checkBox1
);
214 buttonPanel
.add(checkBox2
);
215 buttonPanel
.add(checkBox3
);
217 panel
.add(buttonPanel
);
222 private JPanel
createRadioPanel()
224 JPanel panel
= new JPanel(new BorderLayout());
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
);
231 JPanel buttonPanel
= new JPanel();
232 buttonPanel
.setBorder(BorderFactory
.createTitledBorder("JRadioButton"));
233 this.radio1
= new JRadioButton("Radio 1");
235 this.radio2
= new JRadioButton("Radio 2");
237 this.radio3
= new JRadioButton("Radio 3");
239 ButtonGroup radioGroup
= new ButtonGroup();
240 radioGroup
.add(radio1
);
241 radioGroup
.add(radio2
);
242 radioGroup
.add(radio3
);
244 buttonPanel
.add(radio1
);
245 buttonPanel
.add(radio2
);
246 buttonPanel
.add(radio3
);
248 panel
.add(buttonPanel
);
253 public void actionPerformed(ActionEvent e
)
255 if (e
.getActionCommand().equals("BUTTON_STATE"))
257 button1
.setEnabled(buttonState
.isSelected());
258 button2
.setEnabled(buttonState
.isSelected());
259 button3
.setEnabled(buttonState
.isSelected());
260 button4
.setEnabled(buttonState
.isSelected());
262 else if (e
.getActionCommand().equals("TOGGLE_STATE"))
264 toggle1
.setEnabled(toggleState
.isSelected());
265 toggle2
.setEnabled(toggleState
.isSelected());
266 toggle3
.setEnabled(toggleState
.isSelected());
267 toggle4
.setEnabled(toggleState
.isSelected());
269 else if (e
.getActionCommand().equals("CHECKBOX_STATE"))
271 checkBox1
.setEnabled(checkBoxState
.isSelected());
272 checkBox2
.setEnabled(checkBoxState
.isSelected());
273 checkBox3
.setEnabled(checkBoxState
.isSelected());
275 else if (e
.getActionCommand().equals("RADIO_STATE"))
277 radio1
.setEnabled(radioState
.isSelected());
278 radio2
.setEnabled(radioState
.isSelected());
279 radio3
.setEnabled(radioState
.isSelected());
281 else if (e
.getActionCommand().equals("CLOSE"))
287 public static void main(String
[] args
)
289 SwingUtilities
.invokeLater
294 ButtonDemo app
= new ButtonDemo();
295 app
.initFrameContent();
296 JFrame frame
= new JFrame("ButtonDemo");
297 frame
.getContentPane().add(app
);
299 frame
.setVisible(true);
305 * Returns a DemoFactory that creates a ButtonDemo.
307 * @return a DemoFactory that creates a ButtonDemo
309 public static DemoFactory
createDemoFactory()
311 return new DemoFactory()
313 public JComponent
createDemo()
315 return new ButtonDemo();