Example usage for javax.swing.text MaskFormatter setPlaceholderCharacter

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

Introduction

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

Prototype

public void setPlaceholderCharacter(char placeholder) 

Source Link

Document

Sets the character to use in place of characters that are not present in the value, ie the user must fill them in.

Usage

From source file:Main.java

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);/*from  w ww.j ava2 s  . com*/

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

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Mask Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label;/*from  www . j  a  v a  2  s. co m*/
    JFormattedTextField input;
    JPanel panel;
    MaskFormatter formatter;

    try {
        label = new JLabel("US Phone");
        formatter = new MaskFormatter("'(###')' ###'-####");
        formatter.setPlaceholderCharacter('*');
        input = new JFormattedTextField(formatter);
        input.setColumns(20);
        panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(label);
        panel.add(input);
        frame.add(panel);
    } catch (ParseException e) {
        System.err.println("Unable to add Phone");
    }

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void setValueMask(JFormattedTextField textField) {
    try {//from  w  w w  . j av a  2s . 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:Main.java

MaskFormatter createFormatter(String format) {
    MaskFormatter formatter = null;
    try {// w  ww.j  a  v a 2s . c o m
        formatter = new MaskFormatter(format);
        formatter.setPlaceholderCharacter('.'/*or '0' etc*/);
        formatter.setAllowsInvalid(false); // if needed
        formatter.setOverwriteMode(true); // if needed
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
    }
    return formatter;
}

From source file:Main.java

private void initComponents() {
    JFrame frame = new JFrame("JFormattedTextField Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    MaskFormatter mask = null;
    try {/*from ww  w  .j  a va  2s  .c  o  m*/
        mask = new MaskFormatter("##h##min##s");// the # is for numeric values
        mask.setPlaceholderCharacter('#');
    } catch (ParseException e) {
        e.printStackTrace();
    }
    final JFormattedTextField timeField = new JFormattedTextField(mask);

    // ActionListener for when enter is pressed
    timeField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            Object source = ae.getSource();
            if (source == timeField) {
                // parse to a valid time here
                System.out.println(timeField.getText());
            }
        }
    });
    frame.add(timeField);
    frame.pack();
    frame.setVisible(true);
}