Hi,
I am trying to create timefield using JSpinner similar to TimeField in
Date/Time Properties dialog in Microsoft Windows. Here is the code i have
written
/*****************************************/
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.text.*;
public class MySpinner {
JFormattedTextField tf;
public MySpinner(){
JFrame frame = new JFrame("Spinner");
frame.setDefaultCloseOperation(3);
final SpinnerDateModel model = new SpinnerDateModel();
JSpinner spinner = new JSpinner(model);
JSpinner.DateEditor editor = new
JSpinner.DateEditor(spinner,"HH:ss:mm");
spinner.setEditor(editor);
tf =((JSpinner.DateEditor)spinner.getEditor()).getTextField();
tf.setEditable(true);
tf.setBackground(Color.white);
tf.setSelectionColor(Color.blue);
tf.setSelectedTextColor(Color.white);
DefaultFormatterFactory factory =
(DefaultFormatterFactory)tf.getFormatterFactory();
DateFormatter formatter =
(DateFormatter)factory.getDefaultFormatter();
formatter.setAllowsInvalid(false);
frame.getContentPane().add(spinner, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
public static void main (String args[]) throws Exception {
new MySpinner();
}
}
/*******************************************/
It works just fine when i use up/down keys or spinner at the right to change
the time. But when i try to
modify the time by editing the TimeField it is not behaving as expected.
I want it such that Timefield should allow user to change the time using
Spinner as well as by editing the
timefield. But it should not allow the user to enter invalid time.
Please give me your suggestions. Am I missing something here?
Thanks in advance.
Abhijeet
Dee Dee - 21 Mar 2005 02:01 GMT
Try changing the line:
formatter.setAllowsInvalid(false);
to
formatter.setCommitsOnValidEdit(false);
The second line doesn't check the values that are entered until the
focus leaves the spinner or until the user hits enter.
> Hi,
>
[quoted text clipped - 61 lines]
>
> Abhijeet