Parse Exception MaskFormatter

1.8k Views Asked by At

I'm trying to use MaskFormatter with JFormattedTextField.

Inside my class I have this line of code

JFormattedTextField jtfCNP=new JFormattedTextField(new MaskFormatter(formatCNP));

When I compile, I get the ParseException. The only way around it I found to create the instance of the MaskFormatter inside the constructor of my GUI panel inside a try block like this

    try { format_cnp=new MaskFormatter("# ###### ######");
    jtf_cnp=new JFormattedTextField(format_cnp);
    } catch (ParseException pex) {}
    jtf_cnp.setColumns(20);

I'd like the functionality of the MaskFormatter, but isn't there any way to instantiate it outside a method? Also, is there a better alternative to MaskFormatter?

Edit: the sample code that doesn't work:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import javax.swing.text.*;

    public class test extends JFrame{

        String formatCNP="# ###### ######";
        //components
        JFormattedTextField jtfCNP=new JFormattedTextField(new MaskFormatter(formatCNP));

        public test(){

        super("MaskFormatter Test");
        setSize(300,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jtfCNP.setColumns(20);
        add(jtfCNP);

        setVisible(true);

        }

        public static void main(String[] args){

            test tf=new test();
        }

    }
1

There are 1 best solutions below

0
On BEST ANSWER
  1. MaskFormatter should be created as method, void. etc

  2. read Initial Thread

  3. create an local variable for JFrame, don't to extend whatever in Swing, nor for Top-Level Containers,

  4. search for Java Naming Convention, e.g. class & constructor name should be Test instead of test

.

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.*;

public class Test {

    private String formatString = "##/######";
    private MaskFormatter formatCNP = createFormatter(formatString);
    private JFormattedTextField jtfCNP = new JFormattedTextField(formatCNP);
    private JFrame frame = new JFrame("MaskFormatter Test");

    public Test() {
        jtfCNP.setColumns(20);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(jtfCNP);
        frame.pack();
        frame.setVisible(true);
    }

    MaskFormatter createFormatter(String format) {
        MaskFormatter formatter = null;
        try {
            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;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test tf = new Test();
            }
        });
    }
}