Andreas Leitgeb said:
That's also what I read from the posts.
Unfortunately I don't yet see, *how* to do that.
As I wrote, this is someone else's code, and the original
author doesn't have any inclination to add support for
HHmm input format, and I don't really understand it.
The JFormattedTextField is created such:
m_timeTxt = new JFormattedTextField(new SimpleDateFormat("HH:mm"));
Then, addPropertyChangeListener is called on m_timeTxt,
and the interface-method that is installed does roughly
this:
Date l_time = (Date)m_timeTxt.getValue();
if ( l_time != null ) { ... } // else nothing;
For an input like "1542", l_time turns out to be null.
I don't even see, how I would get to deal with the
entered String, to do any of the re-parsing suggested.
I don't see, where and how to add any other parser to it.
I did read (and understand) your sample, but it didn't
involve the JFormattedTextField-integration.
Probably I miss things, that are blatantly obvious
to everyone used to swing programming.
Combining Mark Space's and Lew's suggestions with my verifier proposal:
<code>
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.Box;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
/** @author John B. Matthews */
public class FormattedDate {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new FormattedDate();
}
});
}
FormattedDate() {
Box form = Box.createVerticalBox();
form.add(new JLabel("Date & Time:"));
DateTimeField dtField = new DateTimeField();
dtField.setValue(new Date());
form.add(dtField);
form.add(new JLabel("Amount:"));
JFormattedTextField amtField = new JFormattedTextField(
NumberFormat.getCurrencyInstance());
amtField.setValue(new Integer(100000));
form.add(amtField);
JFrame frame = new JFrame();
frame.add(form);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class DateTimeField extends JFormattedTextField {
public DateTimeField () {
this.setFormatterFactory(new DefaultFormatterFactory(
new DateFormatter(DateTimeVerifier.getDefaultFormat())));
this.setInputVerifier(new DateTimeVerifier(this));
}
}
class DateTimeVerifier extends InputVerifier {
private static List<SimpleDateFormat> validForms =
new ArrayList<SimpleDateFormat>();
static {
validForms.add(new SimpleDateFormat("dd-MMM-yy HH:mm"));
validForms.add(new SimpleDateFormat("dd-MMM-yy HHmm"));
}
private JFormattedTextField tf;
private Date date;
public DateTimeVerifier(JFormattedTextField tf) {
this.tf = tf;
}
public boolean verify(JComponent input) {
boolean result = false;
if (input == tf) {
String text = tf.getText();
for( SimpleDateFormat format : validForms ) {
try {
date = format.parse(text);
result |= true;
} catch (ParseException pe1) {
result |= false;
}
}
}
return result;
}
public boolean shouldYieldFocus(JComponent input) {
if (verify(input)) {
tf.setValue(date);
return true;
} else return false;
}
public static SimpleDateFormat getDefaultFormat() {
return validForms.get(0);
}
}
</code>
It took me a while to meet the "no side effects" dictum in verify().