title: How to change a FormatedTextField mask dynamically?
body: <p>So I have this FormatedTextField</p>
  
  <pre><code>JFormattedTextField myFtf = new JFormattedTextField();
  </code></pre>
  
  <p>which has the following mask, placed in my application constructor</p>
  
  <pre><code>try {
      myFtf.setFormatterFactory(
          new DefaultFormatterFactory(
             new MaskFormatter("###.###.###-##")));
  } catch (java.text.ParseException ex) {
      ex.printStackTrace();
  }
  </code></pre>
  
  <p>Then, I have a two radio buttons, which should be changing the mask formatter in <code>myFtf</code>. </p>
  
  <p>I have tried the following:</p>
  
  <pre><code>private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
      try {
          myFtf.setFormatterFactory(
              new DefaultFormatterFactory(
                  new MaskFormatter("###.###.###-##")));
      } catch (Exception e) {
          e.printStackTrace();
      }
  }                                                 
  
  private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                      
      try {
          myFtf.setFormatterFactory(
              new DefaultFormatterFactory(
                 new MaskFormatter("##.###.###/####-##")));
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Which works fine, until I try to change their masks <em>when there is input within the text field</em>. In case there is, it doesn't change the mask anymore. Here are a couple of prints:</p>
  
  <hr>
  
  <p>OK scenario:</p>
  
  <p>img a:
  <img src="https://i.stack.imgur.com/ihEDo.png" alt="enter image description here"></p>
  
  <p>switching radio buttons gives me this:</p>
  
  <p>img b:
  <img src="https://i.stack.imgur.com/6sWXz.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>Buggy scenario:</p>
  
  <p>img c:
  <img src="https://i.stack.imgur.com/99UTQ.png" alt="enter image description here"></p>
  
  <p>switching radio buttons gives me this:</p>
  
  <p>img d:
  <img src="https://i.stack.imgur.com/5BD7s.png" alt="enter image description here"></p>
  
  <p>I was expecting <em>img d</em> to be exactly like <em>img a</em></p>
  
  <hr>
  
  <p><strong>How can I dynamically change its mask correctly?</strong></p>