1 /* SpinnerDemo.java -- An example showing various spinners in Swing.
2 Copyright (C) 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;
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;
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;
46 * A simple demo showing various spinners in different states.
48 public class SpinnerDemo
50 implements ActionListener
52 private JCheckBox spinnerState1;
53 private JSpinner spinner1;
54 private JSpinner spinner2;
56 private JCheckBox spinnerState2;
57 private JSpinner spinner3;
58 private JSpinner spinner4;
60 private JCheckBox spinnerState3;
61 private JSpinner spinner5;
62 private JSpinner spinner6;
65 * Creates a new demo instance.
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.
80 void initFrameContent()
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);
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).
97 private void createContent()
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());
107 private JPanel createPanel1()
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);
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);
124 panel.add(controlPanel);
129 private JPanel createPanel2()
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);
137 JPanel controlPanel = new JPanel();
138 controlPanel.setBorder(BorderFactory.createTitledBorder("Date Spinner: "));
139 this.spinner3 = new JSpinner(new SpinnerDateModel(new Date(), null, null,
142 this.spinner4 = new JSpinner(new SpinnerDateModel(new Date(), null, null,
144 this.spinner4.setFont(new Font("Dialog", Font.PLAIN, 20));
146 controlPanel.add(this.spinner3);
147 controlPanel.add(this.spinner4);
149 panel.add(controlPanel);
154 private JPanel createPanel3()
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);
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"}));
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));
172 controlPanel.add(this.spinner5);
173 controlPanel.add(this.spinner6);
175 panel.add(controlPanel);
180 public void actionPerformed(ActionEvent e)
182 if (e.getActionCommand().equals("COMBO_STATE1"))
184 spinner1.setEnabled(spinnerState1.isSelected());
185 spinner2.setEnabled(spinnerState1.isSelected());
187 else if (e.getActionCommand().equals("COMBO_STATE2"))
189 spinner3.setEnabled(spinnerState2.isSelected());
190 spinner4.setEnabled(spinnerState2.isSelected());
192 else if (e.getActionCommand().equals("COMBO_STATE3"))
194 spinner5.setEnabled(spinnerState3.isSelected());
195 spinner6.setEnabled(spinnerState3.isSelected());
197 else if (e.getActionCommand().equals("CLOSE"))
203 public static void main(String[] args)
205 SwingUtilities.invokeLater
210 SpinnerDemo app = new SpinnerDemo();
211 app.initFrameContent();
212 JFrame frame = new JFrame("Spinner Demo");
213 frame.getContentPane().add(app);
215 frame.setVisible(true);
221 * Returns a DemoFactory that creates a SpinnerDemo.
223 * @return a DemoFactory that creates a SpinnerDemo
225 public static DemoFactory createDemoFactory()
227 return new DemoFactory()
229 public JComponent createDemo()
231 return new SpinnerDemo();