[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How do you delete the last character in a JTextArea?

Posted on 2003-03-12
3
Medium Priority
?
2,503 Views
Last Modified: 2012-06-27
Okay, here is the problem.:
I have a mobile phone simulation, where users use individual buttons to input letters onto the screen, by appending the JTextArea.
Now what happens the user makes a mistake - eg: spells a word incorrectly, and wants to delete the last character on the screen :
instead of typing "Hello" it goes on the screen as "Helly"
Now i want to have a button that when hit, erases the last character - "Hell", so that the user may continue.
Is that possible, or does anyone have any advice?
Thanks,
Phoinix
0
Comment
Question by:Phoinix
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
antonsigur earned 100 total points
ID: 8122004
JTextArea text;

....

text.setText(text.getText().substring(0,text.getText().length()-1));

This removes the last character in the textarea by taking it's substring...

You can use this on any text container (TextField ....)
0
 

Author Comment

by:Phoinix
ID: 8122107
Just one line!!! :)  Wow. Thanks a lot, it works perfectly!
Cheers, antonsigur.
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8122225
Jebb.. just one line... hehe...

You could do it in multi-line easy to understand style too:

int numberOfCharsToRemove = 1;
String current = text.getText();
String currentMinus = current.substring(0,current.length()-numberOfCharsToRemove);
text.setText(currentMinus);

:)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

590 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