JFormattedTextField: an input mask (###) ###-#### for a telephone number : Formatted TextField « Swing JFC « Java






JFormattedTextField: an input mask (###) ###-#### for a telephone number

   

import java.awt.Container;
import java.text.ParseException;

import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.text.MaskFormatter;

public class Main {
  public static void main(String args[]) throws ParseException {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

    MaskFormatter mf1 = new MaskFormatter("###-###-###");
    mf1.setPlaceholderCharacter('_');
    JFormattedTextField ftf1 = new JFormattedTextField(mf1);
    content.add(ftf1);

    MaskFormatter mf2 = new MaskFormatter("(###) ###-####");
    JFormattedTextField ftf2 = new JFormattedTextField(mf2);
    content.add(ftf2);
    f.setSize(300, 100);
    f.setVisible(true);
  }
}

   
    
    
  








Related examples in the same category

1.different configurations of JFormattedTextField: Numberdifferent configurations of JFormattedTextField: Number
2.Different configurations of JFormattedTextField: DateDifferent configurations of JFormattedTextField: Date
3.Using an InputVerifier with a formatted textfieldUsing an InputVerifier with a formatted textfield
4.A formatter for regular expressions to be used with JFormattedTextFieldA formatter for regular expressions to be used with JFormattedTextField
5.Field with different formats with focus and withoutField with different formats with focus and without
6.Input: any number of hyphen-delimeted numbers. Output: int arrayInput: any number of hyphen-delimeted numbers. Output: int array
7.A quick demonstration of JFormattedTextFieldA quick demonstration of JFormattedTextField
8.Formatter Factory DemoFormatter Factory Demo
9.Formatted TextField DemoFormatted TextField Demo
10.Accepting Formatted InputAccepting Formatted Input
11.Formatted TextField ExampleFormatted TextField Example
12.Input Verification Demo Input Verification Demo
13.Creating a Text Field to Display and Edit a Phone Number
14.Creating a Text Field to Display and Edit a social security number
15.Make custom Input Text Formatter in Java
16.Support a date with the custom format: 2009-1-1
17.A BigDecimal object custom formatter
18.A decimal number with one digit following the decimal point;
19.Dynamically change the format
20.Creating a Text Field to Display and Edit a Number
21.Creating a Text Field to Display and Edit a Date
22.Format and validate input field in Java Swing
23.Input Verification Dialog Demo Input Verification Dialog Demo
24.A collection of formatted text fields and a button that displays the field values.