?
Solved

How do you delete the last character in a JTextArea?

Posted on 2003-03-12
3
Medium Priority
?
2,212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 Month13 days, 15 hours left to enroll

800 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