?
Solved

JTextArea not resetting

Posted on 2005-04-18
8
Medium Priority
?
555 Views
Last Modified: 2012-08-14
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);
          }
0
Comment
Question by:rkjohnson2005
  • 4
  • 4
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13811861
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.
0
 

Author Comment

by:rkjohnson2005
ID: 13811875
I don't know how to define the action to work on "Enter".
0
 
LVL 92

Expert Comment

by:objects
ID: 13811902
Sorry ignore that, I was thinking you were using a JTextField and not a JTextArea.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rkjohnson2005
ID: 13812049
OK.  Any ideas for the JTextArea?
0
 
LVL 92

Expert Comment

by:objects
ID: 13812064
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.
0
 

Author Comment

by:rkjohnson2005
ID: 13812144
Hmm. . . how do you control the JTextField through the ActionListener?
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13812154
You can add an ActionListener to a JTextField the same way you would with a button.
The listener will get called when enter is pressed in the text field.
0
 

Author Comment

by:rkjohnson2005
ID: 13812172
Wow. . . That is a lot better. . . thank you. . .
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question