1 Reply Latest reply: Mar 27, 2013 11:04 AM by jduprez RSS

    MaskFormatter behavior

    mh*379860*ly
      HI Gentlemen,

      I have been using a Swing API with a DesignGridLayout layout manager. I have several JFormattedTextFields to hold number strings of different lengths, i e only decimal digits are allowed to make up the contents. Now I initialize the fields programmatically and want to modify their contents. Unfortunately, I can append any number of digits at the end at the first time, however a MaskFormatter is attached to each one, e g
      static JTextField kundennummerTemplate = new JFormattedTextField(mf10); where
      static MaskFormatter mf10 = q1.createFormatter("##########"); creates mf10 in a regular way:
      
        static protected MaskFormatter  createFormatter (String s) {
          MaskFormatter formatter = null;
          try {
            formatter = new MaskFormatter(s);
          } 
          catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
          }
          return formatter;
        }
      The fields are static class fields and createFormatter is required, because the new MaskFormatter(...) notation would throw an exception which I can not handle or declare outside of all methods (the fields are class fields). And my Action Listeners are anonymous classes, which would require final local variables--whereas class variables need not be final.

      Anyway, everything is working well except the length limitation. For the second time, when I enter a field again, the mask fires! I can not understand where I am wrong and can not find any explanation in the web.

      Please help me by recommending a working solution.

      Thanks, kind regards

      Miklos HERBOLY
        • 1. Re: MaskFormatter behavior
          jduprez
          Hello Miklos,
          You should check the Swing tutorial http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html and make sure you use JFormattedTextField and MaskFormatter properly.

          As far as asking help here goes, you should post an SSCCE (http://sscce.org).
          - For example I have no idea what q1is and how it "creates a formatter the regular way", nor what a DesignGridLayout is and how it could be related to your problem.
          - Moreover, your disclaimer about using static fields makes me suspect you don't use keyword static properly :)... and using "static fields" improperly across the various widgets might be a reason why the dynamic behavior of the program is surprising.

          Best regards,

          J.