Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Maskformatter with setValidCharacters gets cleared when any JButton is clicked.

  1. #1
    Junior Member
    Join Date
    May 2012
    Thanked 0 Times in 0 Posts

    Default Maskformatter with setValidCharacters gets cleared when any JButton is clicked.

    I am having a problem that I hope someone can help me with because I can't seem to find anything online about it. I am using a MaskFormatter to format a JFormattedTextField and for some reason if I restrict the field to just numbers then whenever any JButton is clicked the field gets cleared even if the JButton doesn't have an actionlistener. It doesn't do this if I allow any character but still use the MaskFormatter to restrict the number of characters. Below is my code.

    MaskFormatter cellNumberFormat = null; //Create a variable to format cell phone numbers
    try {
    cellNumberFormat = new MaskFormatter("(***) ***-****");
    } catch (ParseException ex) {
    Logger.getLogger(UserContacts.class.getName()).log (Level.SEVERE, null, ex);
    cellNumberFormat.setValidCharacters("1234567890"); //Limit what characters can be entered in the cell number field
    cellNumberFie = new JFormattedTextField(cellNumberFormat); //Create a text field for cell numbers that limits how the number can be entered

    So with the way it is written above whenever I click any JButton, even with no actionlistener added to it, then the field gets cleared. But if I comment out the line with the setValidCharacters it doesn't do that. Anyone seen this before?

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: Maskformatter with setValidCharacters gets cleared when any JButton is clicked.

    Is there any reason you are not just using the # character instead of *?

    So you would say: cellNumberFormat = new MaskFormatter("(###) ###-####");
    and then you can get rid of the setValidCharacters() command altogether.

    Here is the API for MaskFormatter: MaskFormatter (Java Platform SE 6)

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. ActionListener not working when button is clicked.
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 19
    Last Post: April 10th, 2012, 04:23 PM
  2. When a button is clicked: get my Data!!!
    By aStudentofJava in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 15th, 2012, 08:45 AM
  3. How to display data against row clicked on html table?
    By reubenmk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 16th, 2012, 03:34 AM
  4. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 02:49 PM
  5. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM