Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / examples / gnu / classpath / examples / swing / SpinnerDemo.java
blob4a05bc482ae0218c542ad48a8c95f06e1b4bdaf7
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)
9 any later version.
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
19 02110-1301 USA.
23 package gnu.classpath.examples.swing;
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;
33 import javax.swing.BorderFactory;
34 import javax.swing.JButton;
35 import javax.swing.JCheckBox;
36 import javax.swing.JFrame;
37 import javax.swing.JPanel;
38 import javax.swing.JSpinner;
39 import javax.swing.SpinnerDateModel;
40 import javax.swing.SpinnerListModel;
41 import javax.swing.SpinnerNumberModel;
42 import javax.swing.UIManager;
43 import javax.swing.plaf.metal.DefaultMetalTheme;
44 import javax.swing.plaf.metal.MetalLookAndFeel;
46 /**
47 * A simple demo showing various spinners in different states.
49 public class SpinnerDemo
50 extends JFrame
51 implements ActionListener
53 private JPanel content;
54 private JCheckBox spinnerState1;
55 private JSpinner spinner1;
56 private JSpinner spinner2;
58 private JCheckBox spinnerState2;
59 private JSpinner spinner3;
60 private JSpinner spinner4;
62 private JCheckBox spinnerState3;
63 private JSpinner spinner5;
64 private JSpinner spinner6;
66 /**
67 * Creates a new demo instance.
69 * @param title the frame title.
71 public SpinnerDemo(String title)
73 super(title);
74 JPanel content = createContent();
75 // initFrameContent() is only called (from main) when running this app
76 // standalone
79 /**
80 * When the demo is run independently, the frame is displayed, so we should
81 * initialise the content panel (including the demo content and a close
82 * button). But when the demo is run as part of the Swing activity board,
83 * only the demo content panel is used, the frame itself is never displayed,
84 * so we can avoid this step.
86 public void initFrameContent()
88 JPanel closePanel = new JPanel();
89 JButton closeButton = new JButton("Close");
90 closeButton.setActionCommand("CLOSE");
91 closeButton.addActionListener(this);
92 closePanel.add(closeButton);
93 content.add(closePanel, BorderLayout.SOUTH);
94 getContentPane().add(content);
97 /**
98 * Returns a panel with the demo content. The panel
99 * uses a BorderLayout(), and the BorderLayout.SOUTH area
100 * is empty, to allow callers to add controls to the
101 * bottom of the panel if they want to (a close button is
102 * added if this demo is being run as a standalone demo).
104 JPanel createContent()
106 if (content == null)
108 content = new JPanel(new BorderLayout());
109 JPanel panel = new JPanel(new GridLayout(3, 1));
110 panel.add(createPanel1());
111 panel.add(createPanel2());
112 panel.add(createPanel3());
113 content.add(panel);
115 return content;
118 private JPanel createPanel1()
120 JPanel panel = new JPanel(new BorderLayout());
121 this.spinnerState1 = new JCheckBox("Enabled", true);
122 this.spinnerState1.setActionCommand("COMBO_STATE1");
123 this.spinnerState1.addActionListener(this);
124 panel.add(this.spinnerState1, BorderLayout.EAST);
126 JPanel controlPanel = new JPanel();
127 controlPanel.setBorder(BorderFactory.createTitledBorder(
128 "Number Spinner: "));
129 this.spinner1 = new JSpinner(new SpinnerNumberModel(5.0, 0.0, 10.0, 0.5));
130 this.spinner2 = new JSpinner(new SpinnerNumberModel(50, 0, 100, 5));
131 this.spinner2.setFont(new Font("Dialog", Font.PLAIN, 20));
132 controlPanel.add(this.spinner1);
133 controlPanel.add(this.spinner2);
135 panel.add(controlPanel);
137 return panel;
140 private JPanel createPanel2()
142 JPanel panel = new JPanel(new BorderLayout());
143 this.spinnerState2 = new JCheckBox("Enabled", true);
144 this.spinnerState2.setActionCommand("COMBO_STATE2");
145 this.spinnerState2.addActionListener(this);
146 panel.add(this.spinnerState2, BorderLayout.EAST);
148 JPanel controlPanel = new JPanel();
149 controlPanel.setBorder(BorderFactory.createTitledBorder("Date Spinner: "));
150 this.spinner3 = new JSpinner(new SpinnerDateModel(new Date(), null, null,
151 Calendar.DATE));
153 this.spinner4 = new JSpinner(new SpinnerDateModel(new Date(), null, null,
154 Calendar.YEAR));
155 this.spinner4.setFont(new Font("Dialog", Font.PLAIN, 20));
157 controlPanel.add(this.spinner3);
158 controlPanel.add(this.spinner4);
160 panel.add(controlPanel);
162 return panel;
165 private JPanel createPanel3()
167 JPanel panel = new JPanel(new BorderLayout());
168 this.spinnerState3 = new JCheckBox("Enabled", true);
169 this.spinnerState3.setActionCommand("COMBO_STATE3");
170 this.spinnerState3.addActionListener(this);
171 panel.add(this.spinnerState3, BorderLayout.EAST);
173 JPanel controlPanel = new JPanel();
174 controlPanel.setBorder(BorderFactory.createTitledBorder("List Spinner: "));
175 this.spinner5 = new JSpinner(new SpinnerListModel(new Object[] {"Red",
176 "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}));
178 this.spinner6 = new JSpinner(new SpinnerListModel(new Object[] {"Red",
179 "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}));
180 this.spinner6.setValue("Yellow");
181 this.spinner6.setFont(new Font("Dialog", Font.PLAIN, 20));
183 controlPanel.add(this.spinner5);
184 controlPanel.add(this.spinner6);
186 panel.add(controlPanel);
188 return panel;
191 public void actionPerformed(ActionEvent e)
193 if (e.getActionCommand().equals("COMBO_STATE1"))
195 spinner1.setEnabled(spinnerState1.isSelected());
196 spinner2.setEnabled(spinnerState1.isSelected());
198 else if (e.getActionCommand().equals("COMBO_STATE2"))
200 spinner3.setEnabled(spinnerState2.isSelected());
201 spinner4.setEnabled(spinnerState2.isSelected());
203 else if (e.getActionCommand().equals("COMBO_STATE3"))
205 spinner5.setEnabled(spinnerState3.isSelected());
206 spinner6.setEnabled(spinnerState3.isSelected());
208 else if (e.getActionCommand().equals("CLOSE"))
210 System.exit(0);
214 public static void main(String[] args)
218 MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
219 UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
221 catch (Exception e) {
222 e.printStackTrace();
224 SpinnerDemo app = new SpinnerDemo("Spinner Demo");
225 app.initFrameContent();
226 app.pack();
227 app.setVisible(true);