Mellow
Mellow

Reputation: 1

Java Moving the Cursor in a JTextField

I have seen questions about moving a cursor using the Robot class by an x and y coordinate, but I am trying to figure out how to reposition a cursor among text in a JTextField.

I have an open parenthesis button that when clicked will take whatever text might be in the JTextField already, concat "(" to it and set this to the JTextField.

I was wondering how I might add the closing parenthesis as well, BUT put the cursor in between the 2 so the user can keep typing uninterrupted. Any suggestions?

Upvotes: 0

Views: 3625

Answers (2)

camickr
camickr

Reputation: 324118

I have an open parenthesis button that when clicked will take whatever text might be in the JTextField already, concat "(" to it and set this to the JTextField.

Don't use getText()/setText() to do this.

Instead you just want to "append" the new text to the text field.

So the logic in your ActonListener might be something like:

int end = textField.getDocument.getLength();
textField.setCaretPosition(end);
textfield.replaceSelection("()");
textField.setCaretPosition(end + 1);

Appending text is more efficient because you only generate a DocumentEvent for the added text.

If you use the getText()/setText() approach then you generate a DocumentEvent for the remove text and then a second event for the text added, which does not reflect what actually happened.

Also, using the length from the Document instead of getting the text is also more efficient since you don't need to actually create a String object.

Upvotes: 0

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

If you want to move the Caret in a JTextField to a specific location from a button then one way to do this would be to set focus upon it first using the JTextField.requestFocus() method then you would need to use the JTextField.setCaretPosition() method to actually relocate the Caret.

If you have a JTextField named jTextField1 and you want to move the Caret to the end of the text contained within then you can use:

jTextField1.requestFocus(); //
jTextField1.setCaretPosition(jTextField1.getText().length());

You need to be careful not to exceed the length of text within the JTextField otherwise an IllegalArgumentException will occur which you can catch by surrounding the above code within a try/catch block. You will also need to consider those times when there might not be any text within the JTextField.

try {
    jTextField1.requestFocus();
    jTextField1.setCaretPosition(jTextField1.getText().length());
}
catch (IllegalArgumentException ex) {
    ///Do Something Here...
}

To Append Brackets to the end of a JTextField then place the Caret between them would be something like this:

String txt = jTextField1.getText(); // Get the text contained in Textfield (if any)
if (txt.equals("")) { txt+= "()"; } // Nothing for text so just add Parenthases 
else { txt+= " ()"; } // Some text there so add a space and Parenthases
jTextField1.setText(txt);
try {
    // Set focus to JTextField
    jTextField1.requestFocus();
    // Move the caret between the Parenthases
    jTextField1.setCaretPosition(jTextField1.getText().length()-1);
}
catch (IllegalArgumentException ex) {
    // Do something here...
}

Upvotes: 2

Related Questions