2
* JSpinField.java - A spin field using a JSpinner (JDK 1.4)
3
* Copyright (C) 2004 Kai Toedter
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
package com.toedter.components;
23
import java.awt.BorderLayout;
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Dimension;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
31
import javax.swing.BorderFactory;
32
import javax.swing.JFrame;
33
import javax.swing.JPanel;
34
import javax.swing.JSpinner;
35
import javax.swing.JTextField;
36
import javax.swing.SpinnerNumberModel;
37
import javax.swing.SwingConstants;
38
import javax.swing.event.CaretEvent;
39
import javax.swing.event.CaretListener;
40
import javax.swing.event.ChangeEvent;
41
import javax.swing.event.ChangeListener;
44
* JSpinField2 is a numeric field with 2 spin buttons to increase or decrease
45
* the value. It has the same interface as the "old" JSpinField but uses a JSpinner
46
* internally (since J2SE SDK 1.4) rather than a scrollbar for emulating the spin buttons.
51
public class JSpinField extends JPanel implements ChangeListener, CaretListener, ActionListener {
52
protected JSpinner spinner;
54
/** the text (number) field */
55
protected JTextField textField;
59
protected Color darkGreen;
62
* Default JSpinField constructor.
65
this(0,Integer.MAX_VALUE);
69
* JSpinField constructor with given minimum and maximum vaues..
71
public JSpinField(int min, int max) {
83
darkGreen = new Color(0, 150, 0);
84
setLayout(new BorderLayout());
85
textField = new JTextField();
86
textField.addCaretListener(this);
87
textField.addActionListener(this);
88
textField.setHorizontalAlignment(SwingConstants.RIGHT);
89
textField.setBorder(BorderFactory.createEmptyBorder());
90
textField.setText(Integer.toString(value));
91
spinner = new JSpinner();
92
spinner.setEditor(textField);
93
spinner.addChangeListener(this);
94
add(spinner, BorderLayout.CENTER);
97
public void adjustWidthToMaximumValue() {
98
JTextField testTextField = new JTextField(Integer.toString(max));
99
int width = testTextField.getPreferredSize().width;
100
int height = testTextField.getPreferredSize().height;
101
textField.setPreferredSize(new Dimension(width,height));
102
textField.revalidate();
106
* Is invoked when the spinner model changes
111
public void stateChanged(ChangeEvent e) {
112
SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
113
int value = model.getNumber().intValue();
118
* Sets the value attribute of the JSpinField object.
122
* @param updateTextField
123
* true if text field should be updated
125
protected void setValue(int newValue, boolean updateTextField, boolean firePropertyChange) {
126
int oldValue = value;
128
if (newValue < min) {
130
} else if (newValue > max) {
136
if (updateTextField) {
137
textField.setText(Integer.toString(value));
138
textField.setForeground(Color.black);
141
if(firePropertyChange) {
142
firePropertyChange("value", oldValue, value);
147
* Sets the value. This is a bound property.
154
public void setValue(int newValue) {
155
setValue(newValue, true, true);
156
spinner.setValue(new Integer(value));
162
* @return the value value
164
public int getValue() {
169
* Sets the minimum value.
172
* the new minimum value
176
public void setMinimum(int newMinimum) {
181
* Returns the minimum value.
183
* @return the minimum value
185
public int getMinimum() {
190
* Sets the maximum value and adjusts the preferred width.
193
* the new maximum value
197
public void setMaximum(int newMaximum) {
202
* Sets the horizontal alignment of the displayed value.
205
* the horizontal alignment
207
public void setHorizontalAlignment(int alignment) {
208
textField.setHorizontalAlignment(alignment);
212
* Returns the maximum value.
214
* @return the maximum value
216
public int getMaximum() {
221
* Sets the font property.
226
public void setFont(Font font) {
227
if (textField != null) {
228
textField.setFont(font);
238
public void setForeground(Color fg) {
239
if (textField != null) {
240
textField.setForeground(fg);
245
* After any user input, the value of the textfield is proofed. Depending on
246
* being an integer, the value is colored green or red.
249
* Description of the Parameter
251
public void caretUpdate(CaretEvent e) {
253
int testValue = Integer.valueOf(textField.getText()).intValue();
255
if ((testValue >= min) && (testValue <= max)) {
256
textField.setForeground(darkGreen);
257
setValue(testValue, false, true);
259
textField.setForeground(Color.red);
261
} catch (Exception ex) {
262
if (ex instanceof NumberFormatException) {
263
textField.setForeground(Color.red);
266
// Ignore all other exceptions, e.g. illegal state exception
273
* After any user input, the value of the textfield is proofed. Depending on
274
* being an integer, the value is colored green or red. If the textfield is
275
* green, the enter key is accepted and the new value is set.
278
* Description of the Parameter
280
public void actionPerformed(ActionEvent e) {
281
if (textField.getForeground().equals(darkGreen)) {
282
setValue(Integer.valueOf(textField.getText()).intValue());
287
* Enable or disable the JSpinField.
290
* The new enabled value
292
public void setEnabled(boolean enabled) {
293
super.setEnabled(enabled);
294
textField.setEnabled(enabled);
295
spinner.setEnabled(enabled);
299
* Returns the year chooser's spinner (which allow the focus to be set to
302
* @return Component the spinner or null, if the month chooser has no
305
public Component getSpinner() {
310
* Returns "JSpinField".
312
* @return the name value
314
public String getName() {
319
* Creates a JFrame with a JSpinField inside and can be used for testing.
322
* The command line arguments
324
public static void main(String[] s) {
325
JFrame frame = new JFrame("JSpinField2");
326
frame.getContentPane().add(new JSpinField());
328
frame.setVisible(true);
b'\\ No newline at end of file'