Example usage for javax.swing.text MaskFormatter setValidCharacters

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

Introduction

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

Prototype

public void setValidCharacters(String validCharacters) 

Source Link

Document

Allows for further restricting of the characters that can be input.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 75);//from w  w w . j a  v a2s .  c o m
    JPanel content = new JPanel(new FlowLayout());
    frame.setContentPane(content);

    MaskFormatter formatter = new MaskFormatter("#");
    formatter.setValidCharacters("123456789");

    JFormattedTextField f1 = new JFormattedTextField(formatter);
    f1.setValue(null);
    f1.setColumns(1);

    content.add(f1);
    frame.setVisible(true);
}

From source file:Main.java

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

From source file:com.unicorn.co226.ui.patient.AddStudentForm.java

/**
 * Creates new form AddStudentForm//from w ww.  j  ava 2 s. com
 */
public AddStudentForm(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedLookAndFeelException ex) {
        Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex);
    }
    this.parent = parent;
    dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    initComponents();
    setLocationRelativeTo(parent);
    medicalPhotos = new File[] {};
    maskFormatterMap = new HashMap<>();

    try {
        maskFormatterMap.put("ENG", new MaskFormatter("E-##-###"));
        MaskFormatter maskFormatter = new MaskFormatter("A-##-###");
        maskFormatter.setValidCharacters("A");
        maskFormatterMap.put("ART", maskFormatter);
        maskFormatterMap.put("SCI", new MaskFormatter("S-##-###"));
        maskFormatterMap.put("MED", new MaskFormatter("M-##-###"));
        maskFormatterMap.put("AHS", new MaskFormatter("AHS-##-###"));
        maskFormatterMap.put("MGT", new MaskFormatter("MGT-##-###"));
        maskFormatterMap.put("VET", new MaskFormatter("V-##-###"));
        maskFormatterMap.put("AGR", new MaskFormatter("AG-##-###"));
        maskFormatterMap.put("DEN", new MaskFormatter("D-##-###"));

    } catch (ParseException ex) {
        Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex);
    }
    //dobDatePicker.setDate(new Date(90, 1, 1));
    dobDatePicker.setFormats(dateFormat, new SimpleDateFormat("yyyy"), new SimpleDateFormat("yyyy/MM/dd"));

    fileChooser = new JFileChooser();
    fileChooser.setMultiSelectionEnabled(true);
    fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "png"));

    try {
        String nextId = IdGen.getNextId("Patient", "id", "P");
        idTextField.setText(nextId);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:es.mityc.firmaJava.libreria.pkcs7.ValidaTarjeta.java

/**
 * This method initializes jNombreTarjetaTextField   
 *    /*w w  w.j a  va2s . co  m*/
 * @return javax.swing.JTextField   
 */
private JFormattedTextField getJNombreTarjetaTextField() {
    if (jNombreTarjetaTextField == null) {

        // El nombre de la tarjeta solo acepta letras y nmeros
        // Se agrega la barra baja como separador
        MaskFormatter formato = new MaskFormatter();
        formato.setValidCharacters(CARACTERES_VALIDOS);
        formato.setAllowsInvalid(false);
        try {
            formato.setMask(MASK);
        } catch (ParseException e) {
            // Nunca ocurre
        }

        jNombreTarjetaTextField = new JFormattedTextField(formato);
        jNombreTarjetaTextField.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);
    }
    return jNombreTarjetaTextField;
}