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:FormatTest.java

public FormatTestFrame() {
    setTitle("FormatTest");
    setSize(WIDTH, HEIGHT);/*from   w  w  w .j  a  v  a 2  s.c  o m*/

    JPanel buttonPanel = new JPanel();
    okButton = new JButton("Ok");
    buttonPanel.add(okButton);
    add(buttonPanel, BorderLayout.SOUTH);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 3));
    add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField.setValue(new Integer(100));
    addRow("Number:", intField);

    JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField2.setValue(new Integer(100));
    intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
    addRow("Number (Commit behavior):", intField2);

    JFormattedTextField intField3 = new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
                protected DocumentFilter getDocumentFilter() {
                    return filter;
                }

                private DocumentFilter filter = new IntFilter();
            });
    intField3.setValue(new Integer(100));
    addRow("Filtered Number", intField3);

    JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField4.setValue(new Integer(100));
    intField4.setInputVerifier(new FormattedTextFieldVerifier());
    addRow("Verified Number:", intField4);

    JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
    currencyField.setValue(new Double(10));
    addRow("Currency:", currencyField);

    JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
    dateField.setValue(new Date());
    addRow("Date (default):", dateField);

    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
    format.setLenient(false);
    JFormattedTextField dateField2 = new JFormattedTextField(format);
    dateField2.setValue(new Date());
    addRow("Date (short, not lenient):", dateField2);

    try {
        DefaultFormatter formatter = new DefaultFormatter();
        formatter.setOverwriteMode(false);
        JFormattedTextField urlField = new JFormattedTextField(formatter);
        urlField.setValue(new URL("http://java.sun.com"));
        addRow("URL:", urlField);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        MaskFormatter formatter = new MaskFormatter("###-##-####");
        formatter.setPlaceholderCharacter('0');
        JFormattedTextField ssnField = new JFormattedTextField(formatter);
        ssnField.setValue("078-05-1120");
        addRow("SSN Mask:", ssnField);
    } catch (ParseException exception) {
        exception.printStackTrace();
    }

    JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
    ipField.setValue(new byte[] { (byte) 130, 65, 86, 66 });
    addRow("IP Address:", ipField);
}

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 "";
    }//  www.  java 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 "";
    }//from ww  w .  ja  va 2  s .co 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;
}

From source file:TextInputDemo.java

protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {/*w  w  w.  ja  v  a2s.com*/
        formatter = new MaskFormatter(s);
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return formatter;
}

From source file:com.mycompany.listBoxer.panel.ListBoxerForm.java

private void AlphabeticCheckBoxActionPerformed(ActionEvent evt) {
    try {//from  w w w  .ja v  a2s . co m
        jTextField1.setValue(StringUtils.EMPTY);
        factory.setDefaultFormatter((new MaskFormatter("????")));
        jTextField1.setFormatterFactory(factory);
    } catch (ParseException e) {
        e.getMessage();
    }
    RangeComboBox.setModel(new DefaultComboBoxModel<String>(
            new String[] { RangeType.ALL.getKey(), RangeType.AM.getKey(), RangeType.NZ.getKey() }));
}

From source file:com.mycompany.listBoxer.panel.ListBoxerForm.java

private void NumericCheckBoxActionPerformed(ActionEvent evt) {
    try {/*from   w  w  w  .  j a va2 s. co  m*/
        jTextField1.setValue(StringUtils.EMPTY);
        factory.setDefaultFormatter(new MaskFormatter("####"));
        jTextField1.setFormatterFactory(factory);
    } catch (ParseException e) {
        e.getMessage();
    }
    RangeComboBox.setModel(
            new DefaultComboBoxModel<String>(new String[] { RangeType.ALL.getKey(), RangeType.NUM1.getKey(),
                    RangeType.NUM2.getKey(), RangeType.NUM3.getKey(), RangeType.NUM4.getKey() }));
}

From source file:com.mycompany.listBoxer.panel.ListBoxerForm.java

private void CombinedCheckBoxActionPerformed(ActionEvent evt) {
    try {//www . j av a  2  s.c o  m
        jTextField1.setValue(StringUtils.EMPTY);
        factory.setDefaultFormatter(new MaskFormatter("****"));
        jTextField1.setFormatterFactory(factory);
    } catch (ParseException e) {
        e.getMessage();
    }
    RangeComboBox.setModel(new DefaultComboBoxModel<String>(new String[] { RangeType.ALL.getKey(),
            RangeType.AM.getKey(), RangeType.NZ.getKey(), RangeType.NUM1.getKey(), RangeType.NUM2.getKey(),
            RangeType.NUM3.getKey(), RangeType.NUM4.getKey() }));
}

From source file:edu.harvard.mcz.imagecapture.GeoreferenceDialog.java

private void init() {
    setBounds(100, 100, 450, 560);/* www  .  j a  v  a2s  .  c om*/
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new GridLayout(0, 2, 0, 0));
    {
        JLabel lblLatitude = new JLabel("Latitude");
        lblLatitude.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPanel.add(lblLatitude);
    }

    textFieldDecimalLat = new JTextField();
    contentPanel.add(textFieldDecimalLat);
    textFieldDecimalLat.setColumns(10);

    JLabel lblLongitude = new JLabel("Longitude");
    lblLongitude.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLongitude);
    {
        textFieldDecimalLong = new JTextField();
        contentPanel.add(textFieldDecimalLong);
        textFieldDecimalLong.setColumns(10);
    }
    {
        JLabel lblDatum = new JLabel("Datum");
        lblDatum.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPanel.add(lblDatum);
    }

    @SuppressWarnings("unchecked")
    ComboBoxModel<String> datumModel = new ListComboBoxModel<String>(LatLong.getDatumValues());
    cbDatum = new JComboBox<String>(datumModel);
    contentPanel.add(cbDatum);

    JLabel lblMethod = new JLabel("Method");
    lblMethod.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblMethod);

    @SuppressWarnings("unchecked")
    ComboBoxModel<String> methodModel = new ListComboBoxModel<String>(LatLong.getGeorefMethodValues());
    cbMethod = new JComboBox<String>(new DefaultComboBoxModel<String>(new String[] { "not recorded", "unknown",
            "GEOLocate", "Google Earth", "Gazeteer", "GPS", "MaNIS/HertNet/ORNIS Georeferencing Guidelines" }));
    cbMethod.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setState();
        }
    });
    contentPanel.add(cbMethod);

    JLabel lblAccuracy = new JLabel("GPS Accuracy");
    lblAccuracy.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblAccuracy);

    txtGPSAccuracy = new JTextField();
    txtGPSAccuracy.setColumns(10);
    contentPanel.add(txtGPSAccuracy);

    JLabel lblNewLabel_1 = new JLabel("Original Units");
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblNewLabel_1);

    comboBoxOrigUnits = new JComboBox<String>();
    comboBoxOrigUnits.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setState();
        }
    });
    comboBoxOrigUnits.setModel(new DefaultComboBoxModel<String>(
            new String[] { "decimal degrees", "deg. min. sec.", "degrees dec. minutes", "unknown" }));
    contentPanel.add(comboBoxOrigUnits);

    lblErrorRadius = new JLabel("Error Radius");
    lblErrorRadius.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblErrorRadius);

    txtErrorRadius = new JTextField();
    txtErrorRadius.setColumns(10);
    contentPanel.add(txtErrorRadius);

    JLabel lblErrorRadiusUnits = new JLabel("Error Radius Units");
    lblErrorRadiusUnits.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblErrorRadiusUnits);

    comboBoxErrorUnits = new JComboBox<String>();
    comboBoxErrorUnits.setModel(new DefaultComboBoxModel<String>(new String[] { "m", "ft", "km", "mi", "yd" }));
    contentPanel.add(comboBoxErrorUnits);

    JLabel lblLatDegrees = new JLabel("Lat Degrees");
    lblLatDegrees.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLatDegrees);

    txtLatDegrees = new JTextField();
    txtLatDegrees.setColumns(4);
    contentPanel.add(txtLatDegrees);

    JLabel lblLatDecMin = new JLabel("Lat Dec Min");
    lblLatDecMin.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLatDecMin);

    txtLatDecMin = new JTextField();
    txtLatDecMin.setColumns(6);
    contentPanel.add(txtLatDecMin);

    JLabel lblLatMin = new JLabel("Lat Min");
    lblLatMin.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLatMin);

    txtLatMin = new JTextField();
    txtLatMin.setColumns(6);
    contentPanel.add(txtLatMin);

    JLabel lblLatSec = new JLabel("Lat Sec");
    lblLatSec.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLatSec);

    txtLatSec = new JTextField();
    txtLatSec.setColumns(6);
    contentPanel.add(txtLatSec);

    JLabel lblLatDir = new JLabel("Lat N/S");
    lblLatDir.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLatDir);

    cbLatDir = new JComboBox<String>();
    cbLatDir.setModel(new DefaultComboBoxModel<String>(new String[] { "N", "S" }));
    contentPanel.add(cbLatDir);

    JLabel lblLongDegrees = new JLabel("Long Degrees");
    lblLongDegrees.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLongDegrees);

    txtLongDegrees = new JTextField();
    txtLongDegrees.setColumns(4);
    contentPanel.add(txtLongDegrees);

    JLabel lblLongDecMin = new JLabel("Long Dec Min");
    lblLongDecMin.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLongDecMin);

    txtLongDecMin = new JTextField();
    txtLongDecMin.setColumns(6);
    contentPanel.add(txtLongDecMin);

    JLabel lblLongMin = new JLabel("Long Min");
    lblLongMin.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLongMin);

    txtLongMin = new JTextField();
    txtLongMin.setColumns(6);
    contentPanel.add(txtLongMin);

    JLabel lblLongSec = new JLabel("Long Sec");
    lblLongSec.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLongSec);

    txtLongSec = new JTextField();
    txtLongSec.setColumns(6);
    contentPanel.add(txtLongSec);

    JLabel lblLongDir = new JLabel("Long E/W");
    lblLongDir.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblLongDir);

    cbLongDir = new JComboBox<String>();
    cbLongDir.setModel(new DefaultComboBoxModel<String>(new String[] { "E", "W" }));
    contentPanel.add(cbLongDir);

    JLabel lblDetBy = new JLabel("Determined By");
    lblDetBy.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblDetBy);

    textFieldDetBy = new JTextField();
    contentPanel.add(textFieldDetBy);
    textFieldDetBy.setColumns(10);

    JLabel lblDetDate = new JLabel("Date Determined");
    lblDetDate.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblDetDate);

    try {
        textDetDate = new JFormattedTextField(new MaskFormatter("####-##-##"));
    } catch (ParseException e1) {
        textDetDate = new JFormattedTextField();
    }
    textDetDate.setToolTipText("Date on which georeference was made yyyy-mm-dd");
    contentPanel.add(textDetDate);

    JLabel lblRef = new JLabel("Reference Source");
    lblRef.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblRef);

    textRefSource = new JTextField();
    contentPanel.add(textRefSource);
    textRefSource.setColumns(10);

    lblNewLabel = new JLabel("Remarks");
    lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(lblNewLabel);

    textFieldRemarks = new JTextField();
    contentPanel.add(textFieldRemarks);
    textFieldRemarks.setColumns(10);

    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            lblErrorLabel = new JLabel("Message");
            buttonPane.add(lblErrorLabel);
        }
        {
            okButton = new JButton("OK");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    lblErrorLabel.setText("");

                    if (saveData()) {
                        setVisible(false);
                    }
                }
            });
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    loadData();
                    setVisible(false);
                }
            });
            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);
        }
    }
}

From source file:org.biojava.bio.view.MotifAnalyzer.java

private JTextField getNumOfMotifs() {
    if (numOfMotifs == null) {
        try {//from   www  .  j a  v a 2 s.c o m
            numOfMotifs = new JFormattedTextField(new MaskFormatter("##"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        numOfMotifs.setText("01");
    }
    return numOfMotifs;
}

From source file:org.biojava.bio.view.MotifAnalyzer.java

private JTextField getNumOfTrials() {
    if (numOfTrials == null) {
        try {// ww w .j av a  2s  .  co  m
            numOfTrials = new JFormattedTextField(new MaskFormatter("##"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        numOfTrials.setText("01");
    }
    return numOfTrials;
}