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(ActionEven t 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.get Text());
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_ME SSAGE);
}
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_ME SSAGE);
}
}
// 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.get Text());
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_ME SSAGE);
}
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(ActionEven
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(
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(
try {
val = Integer.parseInt(input.get
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_ME
}
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_ME
}
}
// Complete the above set of commands for the full replace(int, int) method.
else if(action.equals("replace(
try {
val2 = Integer.parseInt(input.get
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_ME
}
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.
ASKER
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.
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.
ASKER
Hmm. . . how do you control the JTextField through the ActionListener?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow. . . That is a lot better. . . thank you. . .
I'd suggest handling enter in an ActionListener instead of a KeyListener.