title: Checking JTextField Characters
body: <p>I have a program I've written for my kids to practice basic arithmetic. There is a JTextField on a JFrame where the student keys in the answer to a given math problem (like 8+8, they key in 16, Enter). This field accepts only integer values as entries by way of a DocumentFilter. </p>
  
  <p>That part works just fine. What I want to do with this is to avoid having the user have to hit the enter key. Ideally, when the user keys in the first number (1), a check is done to see if 1 == 16. If not, nothing happens. When they subsequently type the 6, the text field displays the number 16. I want to run a check to verify if 16 == 16, and then handle the entry as if the user had also hit the Enter key.</p>
  
  <pre><code>txtAnswer.addKeyListener(new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent e) {               
              int key = e.getKeyCode();
              if (key == KeyEvent.VK_ENTER) {
                  respondToAnswer(isTimed);
              } else {
                  /* Check if the correct answer has been entered */
                  System.out.println(txtAnswer.getText());
                  //int userAnswer = Integer.parseInt(txtAnswer.getText());
                  //if (userAnswer == correctAnswer {
                  //   respondToAnswer(isTimed);
                  //}
              }
          };
      });
  </code></pre>
  
  <p>This code isn't quite working. It's as if it is one character behind. What I mean by that is when the user hits the '1' key (in my 8+8=16 example), the console output is an empty string. When they hit the '6' key, the output is '1'. If they then hit another integer key, the output is '16'. It's always one digit behind. Because of this, I cannot capture the entire contents of the text field to see if it matches the correct answer.</p>
  
  <p>Anyone see what I am missing?</p>
  
  <p>Thanks!</p>