Reputation: 376
Is there an inbuilt function that can remove the last character from a JTextField
, or is the only way to set the string to a new string with the last character removed?
JTextField textfield = new JTextField(sometext);
// What I'm looking for
textfield.removeLastCharacter();
// What I'm using
char[] text = textfield.getText().toCharArray();
String string = "";
for (int i = 0; i < text.length - 1; i++) string += text[i];
textfield.setText(string);
Upvotes: 0
Views: 10225
Reputation: 343
Do this. it works for java programs.
Use your Textfield's name for textField
textField.setText(textField.getText().substring(0, textField.getText ().length() - 1));
Upvotes: 0
Reputation: 260
Just make:
textField.setText(""+textField.getText().substring(0, textField.getText ().length - 1);
Upvotes: 4
Reputation: 324078
Document doc = textField.getDocument();
doc.remove(...);
You will also need to get the length of the Document so you know which character to remove. Check out the Document API for the appropriate method.
Upvotes: 4