Link to home
Start Free TrialLog in
Avatar of Omer-Pitou
Omer-Pitou

asked on

Java - Custom JFormattedTextField - Phone Number

HI,
What am I missing in this custom class? When placed on the container, the format is visible. When I start entering the phone number, the format is destroyed (erased).
package myLibrary;

import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class CustomPhoneNumber extends JFormattedTextField {  
    public CustomPhoneNumber() throws ParseException {     
        MaskFormatter formatter = new MaskFormatter("(###) ###-####");
        formatter.setPlaceholderCharacter('_');
        setFormatter(formatter);
    }       
}

Open in new window

Regards
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Omer-Pitou
Omer-Pitou

ASKER

THANKKKKK YOU VERY MUCH.
It works as expected.
Glad I could help!
Thanx 4 axxepting
Hi,

Is there a way to make getText() only returns what the user enters. Currently if the users enters 909 999 9999, the getText() returns (909) 999-9999 (with the format).

Regards
It's not that difficult to strip off what you don't need:

String formattedTelephoneNumber = "(909) 999-9999";  // = yourField.getText();
String telephoneNumber = formattedTelephoneNumber.replaceAll("[-() ]", "");
System.out.println(telephoneNumber);
You re right. Thanks.
I was thinking that there is an attribute or method that will make it return the user input (same logic as it works for the password field).
Thanks for your prompt reply
I think you could also call getValue() instead of getText(). Just try it.