Example usage for javax.swing.text DefaultFormatterFactory DefaultFormatterFactory

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

Introduction

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

Prototype

public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormat) 

Source Link

Document

Creates a DefaultFormatterFactory with the specified JFormattedTextField.AbstractFormatter.

Usage

From source file:Main.java

public static void setDataMask(JFormattedTextField textField) {
    try {/*w w  w .  j  a v a  2s. co  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 {/*from   w  w  w .j  av  a 2  s .com*/
        MaskFormatter mask = new MaskFormatter("(##) ####-####");
        textField.setFormatterFactory(new DefaultFormatterFactory(mask));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setIsbnMask(JFormattedTextField textField) {
    try {//from  w ww .ja va 2 s . c om
        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 {//  w  w  w  .  j a va 2  s  . co  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

public static final void setMaskFormatter(JFormattedTextField textField, String format) {
    try {//from  w  ww  .j a  v  a  2  s. co m
        MaskFormatter mf = new MaskFormatter(format);
        textField.setFormatterFactory(new DefaultFormatterFactory(mf));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

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 {//from  w ww  .j  a  v  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

private static JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
    JSpinner spinner = new JSpinner(new SpinnerNumberModel());
    JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
    JFormattedTextField textField = jsEditor.getTextField();
    DocumentFilter digitOnlyFilter = new DocumentFilter() {
        @Override//from w  w w .  j  ava2s  . com
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
                throws BadLocationException {
            if (stringContainsOnlyDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
                throws BadLocationException {
            if (stringContainsOnlyDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean stringContainsOnlyDigits(String text) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    };

    NumberFormat format = NumberFormat.getPercentInstance();
    format.setGroupingUsed(false);
    format.setGroupingUsed(true);
    format.setMaximumIntegerDigits(10);
    format.setMaximumFractionDigits(2);
    format.setMinimumFractionDigits(5);
    textField.setFormatterFactory(new DefaultFormatterFactory(new InternationalFormatter(format) {
        @Override
        protected DocumentFilter getDocumentFilter() {
            return digitOnlyFilter;
        }
    }));
    return spinner;
}

From source file:FactoryDemo.java

public static JPanel demo2() {
    // Demo 2: Change the format of a field when the user presses a button.
    // We can't call field.setFormatter() because that's a protected method.
    // (Plus it wouldn't work anyway. The old factory would replace our new
    // formatter with an "old" one next time the field gains or loses
    // focus.)//from  www. j  av a2 s.co  m
    // The thing to do is send a new factory to field.setFormatterFactory().

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 2: change format midstream"));

    MaskFormatter lowercase = null;
    try {
        lowercase = new MaskFormatter("LLLL");
    } catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("Fore");
    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(); // commit current edit (if any)
                MaskFormatter uppercase = new MaskFormatter("UUUU");
                DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
                field.setFormatterFactory(factory);
                change.setEnabled(false);
            } catch (ParseException pe) {
            }
        }
    });

    return pan;
}

From source file:components.IntegerEditor.java

public IntegerEditor(int min, int max) {
    super(new JFormattedTextField());
    ftf = (JFormattedTextField) getComponent();
    minimum = new Integer(min);
    maximum = new Integer(max);

    //Set up the editor for the integer cells.
    integerFormat = NumberFormat.getIntegerInstance();
    NumberFormatter intFormatter = new NumberFormatter(integerFormat);
    intFormatter.setFormat(integerFormat);
    intFormatter.setMinimum(minimum);//from   w ww  . ja  v  a  2  s .  com
    intFormatter.setMaximum(maximum);

    ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
    ftf.setValue(minimum);
    ftf.setHorizontalAlignment(JTextField.TRAILING);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

    //React when the user presses Enter while the editor is
    //active.  (Tab is handled as specified by
    //JFormattedTextField's focusLostBehavior property.)
    ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    ftf.getActionMap().put("check", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (!ftf.isEditValid()) { //The text is invalid.
                if (userSaysRevert()) { //reverted
                    ftf.postActionEvent(); //inform the editor
                }
            } else
                try { //The text is valid,
                    ftf.commitEdit(); //so use it.
                    ftf.postActionEvent(); //stop editing
                } catch (java.text.ParseException exc) {
                }
        }
    });
}

From source file:com.haulmont.cuba.desktop.sys.vcl.DatePicker.DatePicker.java

@Override
public void setFormats(DateFormat... formats) {
    if (formats != null) {
        Contract.asNotNull(formats, "the array of formats " + "must not contain null elements");
    }/*from w  w  w .j  ava2  s.co  m*/
    DateFormat[] old = getFormats();
    for (DateFormat format : formats) {
        format.setLenient(false);
    }
    getEditor().setFormatterFactory(
            new DefaultFormatterFactory(new DatePicker.CustomDatePickerFormatter(formats, getLocale())));
    firePropertyChange("formats", old, getFormats());
}