20.9 Formatting with Regular Expressions


If you are a fan of regular expressions, you might be wondering whether Swing provides any direct support for using regular expressions with JFormattedTextFields. The answer is no. There is no direct support, but it's easy to write your own formatter for regular expressions.

// RegexPatternFormatter.java -- formatter for regular expressions // import javax.swing.*; import javax.swing.text.*; public class RegexPatternFormatter extends DefaultFormatter {   protected java.util.regex.Matcher matcher;   public RegexPatternFormatter(java.util.regex.Pattern regex) {     setOverwriteMode(false);     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); // Honors the valueClass property   }   public static void main(String argv[]) {     // A demo main( ) method to show how RegexPatternFormatter could be used     JLabel lab1 = new JLabel("even length strings:");     java.util.regex.Pattern evenLength = java.util.regex.Pattern.compile("(..)*");     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(noVowels);     noVowelFormatter.setAllowsInvalid(false); // Don't allow user to type vowels.     JFormattedTextField ftf2 = new JFormattedTextField(noVowelFormatter);     JFrame f = new JFrame("RegexPatternFormatter Demo");     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     JPanel pan1 = new JPanel(new java.awt.BorderLayout( ));     pan1.add(lab1, java.awt.BorderLayout.WEST);     pan1.add(ftf1, java.awt.BorderLayout.CENTER);     lab1.setLabelFor(ftf1);     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);     lab2.setLabelFor(ftf2);     f.getContentPane( ).add(pan2, java.awt.BorderLayout.SOUTH);     f.setSize(300, 80);     f.setVisible(true);   } }

Figure 20-5 shows what this example looks like when it runs. Notice that the top field enforces its format only when you tab away from it; if it has an odd-length value at that point, it reverts to the previous value. The bottom field validates as you type; the example shows what happens if you try to type in the entire alphabet.

Figure 20-5. Formatting with regular expressions
figs/swng2.2005.gif

Now you know everything there is to know about JFormattedTextFields, but before we close let's explore one more class: InputVerifier. InputVerifiers are often convenient to use with JFormattedTextFields, which is why we cover them here, but InputVerifiers can be attached to any JComponent, not just to JFormattedTextFields.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net