Dear al,
I have a really irritating problem:
When I run the following code, I can correctly edit all the fields created
by maskformatter, but the dateformat field is always in insert mode, and it
does not display the format characters properly. I am using JDK Version 5.0
release 6, but the problem exists in 1.4.2 as well. It makes the date format
field rather useless. Note that the date is displayed in the correct format,
but when editing it behaves like a plain text field.
Thanks a million for the help.
import javax.swing.*;
import javax.swing.text.DateFormatter;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.Format;
import java.util.Date;
import java.util.Locale;
/**
* Created by IntelliJ IDEA.
* User: User
* Date: Dec 26, 2005
* Time: 10:02:13 AM
* To change this template use File | Settings | File Templates.
*/
public class FormattedSample extends JFrame {
public FormattedSample () throws ParseException {
super("JFormattedTextField Sample");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
// US Social Security number
MaskFormatter mf1 = new MaskFormatter("###-##-####");
mf1.setPlaceholderCharacter('_');
JFormattedTextField ftf2 = new JFormattedTextField(mf1);
content.add(ftf2);
// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
Format format = DateFormat.getDateInstance(DateFormat.MEDIUM);
// format.setLenient(false);
// new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new
DateFormatter(DateFormat.getDateInstance(DateFormat.MEDIUM));
JFormattedTextField ftf1 = new JFormattedTextField(df);
ftf1.setValue(new Date());
content.add(ftf1);
// US telephone number
MaskFormatter mf2 = new MaskFormatter("(###) ###-####");
JFormattedTextField ftf3 = new JFormattedTextField(mf2);
content.add(ftf3);
// f.setSize(300, 100);
pack();
setVisible(true);
setLocationRelativeTo(null);
}
public static void main (String args[]) throws ParseException {
// Where are we?
Locale.setDefault(Locale.UK);
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//createAndShowGUI();
// If we got this far, then we can start main.
try {
new FormattedSample();
} catch (ParseException e) {System.err.println("Error:
"+e.getMessage()); }
}
});
}
}
Ramses the King - 29 Dec 2005 10:06 GMT
Hans E. Kristiansen ha scritto:
> Dear al,
>
[quoted text clipped - 82 lines]
> }
> }
Hi! Sorry for my bad english!
So...the class MaskFormatter must be contained in a cycle "try - catch",
if it does not come executed correctly.!
Bye
Andrea