Example usage for javax.swing.text MaskFormatter MaskFormatter

List of usage examples for javax.swing.text MaskFormatter MaskFormatter

Introduction

In this page you can find the example usage for javax.swing.text MaskFormatter MaskFormatter.

Prototype

public MaskFormatter(String mask) throws ParseException 

Source Link

Document

Creates a MaskFormatter with the specified mask.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Box form = Box.createVerticalBox();
    form.add(new JLabel("Name:"));
    form.add(new JTextField("User Name"));

    form.add(new JLabel("Birthday:"));
    JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
    birthdayField.setValue(new Date());
    form.add(birthdayField);//  www. j  a va2  s.c  o m

    form.add(new JLabel("Age:"));
    form.add(new JFormattedTextField(new Integer(32)));

    form.add(new JLabel("Hairs on Body:"));
    JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
    hairsField.setValue(new Integer(100000));
    form.add(hairsField);

    form.add(new JLabel("Phone Number:"));
    JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
    phoneField.setValue("(314)888-1234");
    form.add(phoneField);

    JFrame frame = new JFrame("User Information");
    frame.getContentPane().add(form);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void setIsbnMask(JFormattedTextField textField) {
    try {/*from w  w w.  j ava2s.  c o m*/
        MaskFormatter mask = new MaskFormatter("###-#-##-######-#");
        textField.setFormatterFactory(new DefaultFormatterFactory(mask));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setFoneMask(JFormattedTextField textField) {
    try {// w w w  .  jav  a2  s. com
        MaskFormatter mask = new MaskFormatter("(##) ####-####");
        textField.setFormatterFactory(new DefaultFormatterFactory(mask));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setDataMask(JFormattedTextField textField) {
    try {//from  w  w  w.  j a  v  a  2 s. co m
        MaskFormatter mask = new MaskFormatter("##/##/####");
        textField.setFormatterFactory(new DefaultFormatterFactory(mask));
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void setValueMask(JFormattedTextField textField) {
    try {/*from w  w w.j a  va 2s.  c  om*/
        MaskFormatter mask = new MaskFormatter("R$ ###.##");
        mask.setValidCharacters("0123456789");
        mask.setPlaceholderCharacter('0');
        textField.setFormatterFactory(new DefaultFormatterFactory(mask));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static JPanel demo1() {
    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 1: format toggles with focus"));

    MaskFormatter withFocus = null, withoutFocus = null;
    try {/* w ww . j  av a 2  s . c  om*/
        withFocus = new MaskFormatter("LLLL");
        withoutFocus = new MaskFormatter("UUUU");
    } catch (ParseException pe) {
    }

    DefaultFormatterFactory factory = new DefaultFormatterFactory(withoutFocus, null, withFocus);

    JFormattedTextField field = new JFormattedTextField(factory);
    field.setValue("Four");
    pan.add(field, BorderLayout.CENTER);

    return pan;
}

From source file:MainClass.java

public static JPanel demo1() {
    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("change format midstream"));

    MaskFormatter lowercase = null;
    try {/* w w w  .  jav a 2 s  . c  o m*/
        lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("lower case");
    pan.add(field, BorderLayout.CENTER);

    final JButton change = new JButton("change format");
    JPanel changePanel = new JPanel();
    changePanel.add(change);
    pan.add(changePanel, BorderLayout.SOUTH);

    change.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                field.commitEdit();
                MaskFormatter uppercase = new MaskFormatter("UUUU");
                DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
                field.setFormatterFactory(factory);
                change.setEnabled(false);
            } catch (ParseException pe) {
            }
        }
    });

    return pan;
}

From source file:Main.java

public static final JFormattedTextField createMaskFormattedTextField(String format) {
    try {//  w  w w .j  a v a  2  s  .  com
        MaskFormatter mf = new MaskFormatter(format);
        JFormattedTextField field = new JFormattedTextField(mf);
        return field;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static final void setMaskFormatter(JFormattedTextField textField, String format) {
    try {/*from w  w  w  . j av  a2 s  .  c om*/
        MaskFormatter mf = new MaskFormatter(format);
        textField.setFormatterFactory(new DefaultFormatterFactory(mf));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:FactoryDemo.java

public static JPanel demo1() {
    // Demo 1: field with different formats with focus and without

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 1: format toggles with focus"));

    MaskFormatter withFocus = null, withoutFocus = null;
    try {/*  ww w . ja  va2  s  . c om*/
        withFocus = new MaskFormatter("LLLL");
        withoutFocus = new MaskFormatter("UUUU");
    } catch (ParseException pe) {
    }

    DefaultFormatterFactory factory = new DefaultFormatterFactory(withoutFocus, null, withFocus);

    JFormattedTextField field = new JFormattedTextField(factory);
    field.setValue("Four");
    pan.add(field, BorderLayout.CENTER);

    return pan;
}