A formatter for regular expressions to be used with JFormattedTextField : Formatted TextField « Swing JFC « Java

A formatter for regular expressions to be used with JFormattedTextField

A formatter for regular expressions to be used with JFormattedTextField
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
// RegexPatternFormatter.java
//A formatter for regular expressions to be used with JFormattedTextField.

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.DefaultFormatter;

public class RegexPatternFormatter extends DefaultFormatter {

  protected java.util.regex.Matcher matcher;

  public RegexPatternFormatter(java.util.regex.Pattern regex) {
    matcher = regex.matcher(""); // create a Matcher for the regular
                   // expression

  public Object stringToValue(String string) throws java.text.ParseException {
    if (string == null)
      return null;
    matcher.reset(string); // set 'string' as the matcher's input

    if (!matcher.matches()) // Does 'string' match the regular expression?
      throw new java.text.ParseException("does not match regex", 0);

    // If we get this far, then it did match.
    return super.stringToValue(string); // will honor the 'valueClass'
                      // property

  public static void main(String argv[]) {
    // a demo main() to show how RegexPatternFormatter could be used

    JLabel lab1 = new JLabel("even length strings:");
    java.util.regex.Pattern evenLength = java.util.regex.Pattern
    JFormattedTextField ftf1 = new JFormattedTextField(
        new RegexPatternFormatter(evenLength));

    JLabel lab2 = new JLabel("no vowels:");
    java.util.regex.Pattern noVowels = java.util.regex.Pattern.compile(
        "[^aeiou]*", java.util.regex.Pattern.CASE_INSENSITIVE);
    RegexPatternFormatter noVowelFormatter = new RegexPatternFormatter(
    noVowelFormatter.setAllowsInvalid(false); // don't allow user to type
                          // vowels
    JFormattedTextField ftf2 = new JFormattedTextField(noVowelFormatter);

    JFrame f = new JFrame("RegexPatternFormatter Demo");
    JPanel pan1 = new JPanel(new java.awt.BorderLayout());
    pan1.add(lab1, java.awt.BorderLayout.WEST);
    pan1.add(ftf1, java.awt.BorderLayout.CENTER);
    f.getContentPane().add(pan1, java.awt.BorderLayout.NORTH);
    JPanel pan2 = new JPanel(new java.awt.BorderLayout());
    pan2.add(lab2, java.awt.BorderLayout.WEST);
    pan2.add(ftf2, java.awt.BorderLayout.CENTER);
    f.getContentPane().add(pan2, java.awt.BorderLayout.SOUTH);
    f.setSize(300, 80);


Related examples in the same category

1.different configurations of JFormattedTextField: Numberdifferent configurations of JFormattedTextField: Number
2.Different configurations of JFormattedTextField: DateDifferent configurations of JFormattedTextField: Date
3.JFormattedTextField: an input mask (###) ###-#### for a telephone number
4.Using an InputVerifier with a formatted textfieldUsing an InputVerifier with a formatted textfield
5.Field with different formats with focus and withoutField with different formats with focus and without
6.Input: any number of hyphen-delimeted numbers. Output: int arrayInput: any number of hyphen-delimeted numbers. Output: int array
7.A quick demonstration of JFormattedTextFieldA quick demonstration of JFormattedTextField
8.Formatter Factory DemoFormatter Factory Demo
9.Formatted TextField DemoFormatted TextField Demo
10.Accepting Formatted InputAccepting Formatted Input
11.Formatted TextField ExampleFormatted TextField Example
12.Input Verification Demo Input Verification Demo
13.Creating a Text Field to Display and Edit a Phone Number
14.Creating a Text Field to Display and Edit a social security number
15.Make custom Input Text Formatter in Java
16.Support a date with the custom format: 2009-1-1
17.A BigDecimal object custom formatter
18.A decimal number with one digit following the decimal point;
19.Dynamically change the format
20.Creating a Text Field to Display and Edit a Number
21.Creating a Text Field to Display and Edit a Date
22.Format and validate input field in Java Swing
23.Input Verification Dialog Demo Input Verification Dialog Demo
24.A collection of formatted text fields and a button that displays the field values.