title: JFormattedTextfield only validate when pressed enter
body: <p>For a task I need to make a JFormattedTextField with the following behavior:</p>
  
  <ul>
  <li>If value is edited and isn't equal to the last validated value the background must become yellow. </li>
  <li>Value validation may take place at any time</li>
  <li>If focus is lost nothing should happen (if background is yellow it should remain yellow,...)</li>
  <li>Action should be taken when Enter is pressed</li>
  </ul>
  
  <p>I can't seem to find the correct combination of Listeners to accomplish this. I tried using <code>KeyAdapter</code>, <code>InputVerifier</code> and <code>PropertyChangeListener</code>but that gives me very ugly code wich only works for 80%.</p>
  
  <p>How should this be done?</p>
  
  <p>Edit: I wrote a small example:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Container;
  import java.awt.event.ActionEvent;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.text.ParseException;
  
  import javax.swing.AbstractAction;
  import javax.swing.InputVerifier;
  import javax.swing.JButton;
  import javax.swing.JComponent;
  import javax.swing.JFormattedTextField;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  public class Test extends JPanel {
  
      private JFormattedTextField field;
      private JLabel label;
      private JButton btn;
  
      public Test() {
          super(new BorderLayout());
  
          label = new JLabel("Enter a float value:");
          btn = new JButton(new AbstractAction("Print to stdout"){
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  System.out.println(field.getValue());
              }
  
          });
          field = new JFormattedTextField(new Float(9.81));
  
          field.addKeyListener(new KeyAdapter(){
  
              @Override
              public void keyPressed(KeyEvent e){
                  field.setBackground(Color.YELLOW);
              }
  
              @Override 
              public void keyTyped(KeyEvent e){
                  if(e.getKeyCode() == KeyEvent.VK_ENTER){
                      try{
                          field.commitEdit();
                          field.setBackground(Color.WHITE);
                      }catch(ParseException e1){
                          field.setBackground(Color.RED);
                      }
                  }
              }
          });
  
          field.setInputVerifier(new InputVerifier(){
  
              @Override
              public boolean verify(JComponent comp) {
                  try{
                      field.commitEdit();
                      field.setBackground(Color.YELLOW);
                      return true;
                  }catch(ParseException e){
                      field.setBackground(Color.RED);
                      return false;
                  }
              }
  
          });
  
          add(label, BorderLayout.NORTH);
          add(field, BorderLayout.CENTER);
          add(btn, BorderLayout.SOUTH);
      }
  
  
      public static void main(String[] args) {
          JFrame window = new JFrame("InputVerifier test program");
          Container cp = window.getContentPane();
          cp.add(new Test());
          window.pack();
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setVisible(true);
      }
  }
  </code></pre>
  
  <p>This almost does everything I want. But the problem is the ENTER key is never caught. I think it is consumed before it reaches my KeyListener, but how can I prevent this?</p>
  
  <p>Even if this can be prevented, I still have the feeling there should be a cleaner why to accomplish what above code does.</p>