Link to home
Start Free TrialLog in
Avatar of rkjohnson2005
rkjohnson2005

asked on

JTextArea not resetting

Hello. I have a GUI interface setup and all the methods work find except for one minor glitch.
I have a label (message) telling the user what to do and a JTextArea ("input") for inputting data. Another JTextArea ("text") is used for output.   I want to use a replace method that replaces all instances of an integer with another in a linked list. The problem is: between the first input and the second input, the JTextArea will not reset to one line.  I use the following code to attempt this:
              input.setText("");
This works for the other methods, but here the cursor stays on a second line in the text area.  So when I try to parse the integer, it throws an exception. It works fine when I hit backspace and return to the first line but  I can't seem to find the problem. . .

Here is the code:
The method comes from a menu, so I have the actionPerformed part defined.  Then it passes to a KeyListener class defined below for the first input. Then it passes again to the same KeyListener class for the second input.

actionPerformed(ActionEvent e) {
action = e.getActionCommand();
/ Replace each instance of an element with a different value. This requires 2
// input elements from the user.  For each input value, it will wait fro the
// user to hit the "enter" key and then finish within the EnterListener class.
        else if(action.equals("replace(int, int)")) {
          message.setText("Enter the value to be replaced");
          input.setEditable(true);
        }
}

    private class EnterListener implements KeyListener {
      public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_ENTER)
// Complete the "replace(int, int)" commands.  If the value is found in the
// list, the program will pass to another set of commands below.
          if(action.equals("replace(int, int)")) {
            try {
              val = Integer.parseInt(input.getText());
              input.setText("");
              if(list.search(val) == false) {
                text.append("\nThat value was not found in the list or the list is empty.");
                message.setText(DEFAULT_MESSAGE);
              }
              else {
                input.setEditable(true);
                message.setText("Enter the new value:");
                action = "replace(int, int)2";
              }
            }
            catch(Exception error) {
              text.append("\nInput error - please retry the method.");
              input.setText("");
              input.setEditable(false);
              message.setText(DEFAULT_MESSAGE);
            }
          }
// Complete the above set of commands for the full replace(int, int) method.
          else if(action.equals("replace(int, int)2")) {
            try {
              val2 = Integer.parseInt(input.getText());
              list.replace(val, val2);
              text.append("\nAll instances of " + val + " were replaced with " + val2);
            }
            catch (Exception error) {
              text.append("\nInput error - please retry the method.");
            }
            input.setText("");
            input.setEditable(false);
            message.setText(DEFAULT_MESSAGE);
          }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Think thats because the enter has not been insterted into the text area at the point your listener is called.
I'd suggest handling enter in an ActionListener instead of a KeyListener.
Avatar of rkjohnson2005
rkjohnson2005

ASKER

I don't know how to define the action to work on "Enter".
Sorry ignore that, I was thinking you were using a JTextField and not a JTextArea.
OK.  Any ideas for the JTextArea?
Am thinking about it, btw any reason for using a JTextArea when you are only inputting a number?

Perhaps a DocumentListener may be a better approach than a KeyListener, that would allow you to control whether the cr got inserted in the document or not.
Hmm. . . how do you control the JTextField through the ActionListener?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow. . . That is a lot better. . . thank you. . .