Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 15th, 2006, 07:28 AM
Authorized User
Join Date: Dec 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input validation in JFormattedTextField

I need to restrict the size as well as check the Input characters entered in the JFormattedTextField.
I know that to restrict the size I can use
 t1= new JFormattedTextField(new MaskFormatter("*****"));

For checking the characters entered in the Field, here is the class.
class FilteredFormatter extends DefaultFormatter {
      protected DocumentFilter getDocumentFilter() {
        return new InputFilter();

    class InputFilter extends DocumentFilter {

      String domain = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789.";

 public void insertString(DocumentFilter.FilterBypass fb,
                               int offset,
                               String str,
                               AttributeSet attr) throws BadLocationException

        replace(fb, offset, 0, str, attr);

public void replace(DocumentFilter.FilterBypass fb,
                          int offset,
                          int length,
                          String str,
                          AttributeSet attrs) throws BadLocationException {
        char[] source = str.toLowerCase().toCharArray();
        char[] result = new char[source.length];
        int j = 0;

        for(int i = 0; i < result.length; i++)
          if(domain.indexOf(source[i]) != -1)
            result[j++] = source[i];

        fb.replace(offset, length, new String(result, 0, j), attrs);

I can do that by creating the new Instance
t1= new JFormattedTextField(new FilteredFormatter());

I want both size and charvalidation. With this only one is possible at a time. How do I get both with the above code?
Old February 17th, 2006, 03:58 AM
Authorized User
Join Date: Dec 2005
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts

Jmask from http://www.javawisdom.com/ can meet your need.

JAVA Email
Old November 14th, 2006, 01:50 AM
Registered User
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jaya1511

Try this, does both size and char validation
MaskFormatter formatter = new MaskFormatter("*****");
formatter.setValidCharacters("abcdefghijklmnopqrst uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.");
        t1 = new JFormattedTextField(formatter);

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For JFormattedTextField madhaviv Java Basics 0 August 3rd, 2008 10:05 PM
Input Validation sams ASP.NET 2.0 Professional 4 June 12th, 2007 03:29 AM
Input validation shs BOOK: Beginning ASP.NET 1.0 14 January 6th, 2005 01:03 PM
Input Validation Maxood C++ Programming 11 August 6th, 2004 02:55 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.