Example usage for javax.swing.text MaskFormatter install

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

Introduction

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

Prototype

public void install(JFormattedTextField ftf) 

Source Link

Document

Installs the DefaultFormatter onto a particular JFormattedTextField.

Usage

From source file:br.com.pontocontrol.controleponto.util.SwingUtils.java

public static void setMask(JFormattedTextField field, String mask) {
    try {//ww  w.j a va 2s.c o m
        MaskFormatter maskFormatter = new MaskFormatter(mask);
        maskFormatter.setPlaceholder("");
        maskFormatter.install(field);
    } catch (ParseException ex) {
        LOG.log(Level.SEVERE, "Erro ao definir mscara no campo", ex);
    }
}

From source file:br.com.itfox.utils.Utils.java

public static String formatCpfCnpj(String value) {
    String maskCpf = "###.###.###-##";
    String maskCnpj = "##.###.###/####-##";
    String maskCep = "##.###-###";
    String mask = "";
    if (value == null) {
        return "";
    }/*w ww.  j  a  v  a  2  s.c  o m*/
    if (value.length() == 14) {
        mask = maskCnpj;
    } else if (value.length() == 11) {
        mask = maskCpf;
    } else if (value.length() == 8) {
        mask = maskCep;
    }
    try {
        MaskFormatter formatter = new MaskFormatter(mask);
        JFormattedTextField textField = new JFormattedTextField();
        formatter.install(textField);
        textField.setText(value);
        value = textField.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:br.com.itfox.utils.Utils.java

public static String formatTelephone(String value) {
    String maskTel = "####-####";
    String maskCel = "#####-####";
    String maskDDD = "(##) ";
    String mask = "";
    if (value == null) {
        return "";
    }//  w ww.ja v  a 2 s  .  c  o  m
    if (value.length() == 9) {
        mask = maskCel;
    } else if (value.length() == 8) {
        mask = maskTel;
    } else if (value.length() == 2) {
        mask = maskDDD;
    }

    try {
        MaskFormatter formatter = new MaskFormatter(mask);
        JFormattedTextField textField = new JFormattedTextField();
        formatter.install(textField);
        textField.setText(value);
        value = textField.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}