title: JFormattedTextField with MaskFormatter and DateFormat
body: <p>after reading <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html" rel="nofollow noreferrer">this article</a> and <a href="https://stackoverflow.com/questions/12655811/how-to-make-a-jtextfield-having-a-fixed-date-format">this question</a> I tried to make a combo of the two: a JFormattedTextField that always shows the slashes in the correct positions and that automatically parses a Date object.</p>
  
  <p>The code I came up with is the following:</p>
  
  <pre><code>private DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
  textField_DaRiassuntoIncassi = new JFormattedTextField(df);
  textField_ARiassuntoIncassi = new JFormattedTextField(df);
  textField_DaScadenze = new JFormattedTextField(df);
  textField_AScadenze = new JFormattedTextField(df);
  textField_DaRiassuntoIncassi.setColumns(10);
  textField_ARiassuntoIncassi .setColumns(10);
  textField_DaScadenze .setColumns(10);
  textField_AScadenze .setColumns(10);
  
  try
  {
      MaskFormatter dateMask = new MaskFormatter("##/##/####");
      dateMask.install(textField_DaRiassuntoIncassi);
      dateMask.install(textField_ARiassuntoIncassi);
      dateMask.install(textField_DaScadenze);
      dateMask.install(textField_AScadenze);
  }
  catch(ParseException ex)
  {
      ex.printStackTrace();
  }
  </code></pre>
  
  <p>The problem is that when I click on the textfield to input the value, when I type the two slashes are moved as I type, instead I would like them to remain fixed (just like when the key "Insert" on the keyboard is pressed).
  If I put the MaskFormatter in the constructor that problem goes away, but I can enter any number I want in the textfield, such as "99/00/9874" and the component tells me that's an ok value, because I don't know where to plug the SimpleDateFormat.</p>
  
  <p>My last resort is to put the MaskFormatter in the JFormattedTextField constructor, get the text with the getText() method, try to parse a date with the DateFormat and in case of error do something, but I think there's a clever way to do this.
  I tried using the method </p>
  
  <pre><code>textField_AScadenze.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(new SimpleDateFormat("dd/MM/yyyy"))));
  </code></pre>
  
  <p>but as soon as I don't insert anything and click out the slashes are gone.
  Please help.
  Thank you</p>