aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Edit masks in text fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Edit masks in text fields" Watch "Edit masks in text fields" New topic
Author

Edit masks in text fields

Frank Lautenbach
Greenhorn

Joined: Nov 30, 2004
Posts: 4
Hi everyone,

I'm brand new to this forum and relatively new to Java. I'm currently supporting an application that runs on a PDA using the NSI CrEme runtime. (The person who developed it is gone, and it's mine to support ... which is OK because I want to learn Java.)

Anyway, here's my question. The CrEme runtime is compatible with JDK 1.1.8. I can't use Swing ... just AWT. I have all kinds of input fields including date/time, phone number, etc. I'm familiar with edit masks in other languages and applications I've worked with in the past. Is there anything available in Java at this level of the JDK? I'm really looking for some sample source that might help me out. I've found sample source on the web in C and C++ that implement maskable edit fields, but nothing in Java. Can anyone help me out?

Thanks
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
Hi,
What are these maskable edit fields?
- Sai
Frank Lautenbach
Greenhorn

Joined: Nov 30, 2004
Posts: 4
Sai:

An example of a text field where an edit mask would be helpful is a date field. I would like to force the input to be ##/##/#### where '#' signifies a numeric digit [0-9] and the '/' characters are already present in the field (i.e. do not have to be typed.) The text field would look something like __/__/__. If the user typed the date 12/01/2004, he would type 1 and then 2. The caret would then skip past the '/' sign allow the 0 and 1 to be typed, etc. Other types of fields that come to mind that could use edit masks are phone number fields and time fields. This feature is pretty common on handheld/PDA applications, mainly because characters such as '/' are awkward to get to. They often require some sort of shifting of the keyboard to a different mode --- not very user friendly.

Frank
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
With a restriction to AWT you may have some work to do. I can point you to j2se 1.4+ resources for this:
  • Check out reply 4 on this thread for an example of Date formatting.
  • JFormattedTextField class
  • MaskFormatter class
  • You can look in the source code for the MaskFormatter and JFormattedTextField classes to get some ideas about implementation.
    Frank Lautenbach
    Greenhorn

    Joined: Nov 30, 2004
    Posts: 4
    Craig,

    Thanks for the help! I'll check it out.

    Frank
    Sainath Veepuri
    Ranch Hand

    Joined: Sep 25, 2003
    Posts: 49
    Hi Frank,
    Thanks a bunch for your explanation.
    Now have a glance below, and try it out.
    Hope this fits your need.
    Bye,
    - SAi
    =======================================================================
    =======================================================================
    This example uses a JFormattedTextField to allow the display and editing of a date. By default, when the component loses the focus and the modified value is a valid date, the modified value is saved. Otherwise, if the modified value is not a valid date, the modified value is discarded and the old value is displayed.
    // Support a date in the MEDIUM format in the current locale;
    // see e322 Formatting and Parsing a Date Using Default Formats.
    // For Locale.ENGLISH, the format would be Feb 8, 2002.
    JFormattedTextField tft1 = new JFormattedTextField(new Date());

    // Support a date in the SHORT format using the current locale.
    // For Locale.ENGLISH, the format would be 2/8/02.
    JFormattedTextField tft2 = new JFormattedTextField(DateFormat.getDateInstance(DateFormat.SHORT));
    tft2.setValue(new Date());

    // Support a date with the custom format: 2002-8-2
    JFormattedTextField tft3 = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d"));
    tft3.setValue(new Date());
    // See also e320 Formatting a Date Using a Custom Format

    // Retrieve the date from the text field
    Date date = (Date)tft3.getValue();

    The following example demonstrates how to dynamically change the format:
    // Change the format to: 2/8/2002
    DateFormatter fmt = (DateFormatter)tft3.getFormatter();
    fmt.setFormat(new SimpleDateFormat("d/M/yyyy"));

    // Reformat the display
    tft3.setValue(tft3.getValue());
    =======================================================================
    =======================================================================
    This example uses a JFormattedTextField to allow the display and editing of certain fixed-string patterns. By default, when the component loses the focus and the modified value is valid, the modified value is saved. Otherwise, if the modified value is not valid, the modified value is discarded and the old value is displayed.
    The pattern is specified using one of the following characters: # represents a decimal digit, H represents a hex digit, U represents an uppercase letter, L represents a lowercase letter, A represents a number or letter, ? represents a letter in any case, and * represents any character. Any other character in the pattern represents itself. If it is necessary to use one of the special characters, it can be escaped by preceding it with a quote (').

    MaskFormatter fmt = null;

    // A phone number
    try {
    fmt = new MaskFormatter("###-###-####");
    } catch (java.text.ParseException e) {
    }
    JFormattedTextField tft1 = new JFormattedTextField(fmt);


    // A social security number
    try {
    fmt = new MaskFormatter("###-##-####");
    } catch (java.text.ParseException e) {
    }
    JFormattedTextField tft2 = new JFormattedTextField(fmt);

    The spot where a character or digit is expected is called a placeholder. By default, a placeholder is represented with a space character. The space is automatically replaced as the user fills in the field. This example demonstrates how to use an asterisk as the placeholder character.
    // A social security number
    fmt.setPlaceholderCharacter('*');
    JFormattedTextField tft3 = new JFormattedTextField(fmt);

    =======================================================================
    =======================================================================
    Frank Lautenbach
    Greenhorn

    Joined: Nov 30, 2004
    Posts: 4
    Hi Sai:

    Thanks for the response. Unfortunately, I can't use Swing. The NSIcom CrEme java runtime I'm using is jdk v1.1.8 compatible. I'm planning on migrating to the J2ME J9 runtime some time in the future, but I'm restricted to AWT in that environment also.

    The world of PDA development has it's own set of unique challenges.

    Thanks

    Frank
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Edit masks in text fields