1

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();
        }

    }
user3653831
  • 235
  • 6
  • 19
  • no idea from code posted here, starting with `new MaskFormatter(formatCNP)` doesn't meets with `format_cnp=new MaskFormatter("# ###### ######");` – mKorbel Aug 14 '14 at 12:19
  • +1 for edit with an SSCCE/MCVE, see my answer to your question – mKorbel Aug 14 '14 at 13:28
  • _"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"_ - EEK! So you wrap it in a try/catch with an empty catch? Horrible. What if you _do_ get the parse exception? Which you probably are, but you can't tell, because you're swallowing the exception. The reason you need to handle the exception is because if the String you pass has illegal characters (which yours does - space not permitted) it will throw an exception. – Paul Samsotha Aug 14 '14 at 13:43
  • Look at [the api](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html#MaskFormatter%28java.lang.String%29) for more detailes – Paul Samsotha Aug 14 '14 at 13:43

1 Answers1

2
  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();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319