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).
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);
}
}
Regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad I could help!
Thanx 4 axxepting
Thanx 4 axxepting
ASKER
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
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.r eplaceAll( "[-() ]", "");
System.out.println(telepho neNumber);
String formattedTelephoneNumber = "(909) 999-9999"; // = yourField.getText();
String telephoneNumber = formattedTelephoneNumber.r
System.out.println(telepho
ASKER
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 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.
ASKER
It works as expected.