This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTextField + JTextArea -> Problem with backspace-ing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField + JTextArea -> Problem with backspace-ing" Watch "JTextField + JTextArea -> Problem with backspace-ing" New topic
Author

JTextField + JTextArea -> Problem with backspace-ing

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I was working on simple Swing application, and I got stuck - again! The application is something similar to Windows Command Prompt.
This is the deal now:
I have a JTextField where user can enter command; the response is shown in the JTextArea above it.
I am waiting for the command in the field until user presses Enter key. After that some logic is invoked, and result is displayed inside of JTextArea. I also have one more event assocciated with JTextField - when any key is pressed, character pressed is appended to text in JTextArea, so it lookes like user is typing command in JEditorPane. And now, I don't know how can I make application remove last entered characted from JTextArea if user presses Backspace (just in case user wants to change/correct entered command)?
One way might be to retreive all the text from text area, then "manually" remove last character and then set new text again. But, I was wondering if there is a "more ellegant" way to do that?

Thanks!


The quieter you are, the more you are able to hear.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Get the field's Document and use remove and getLength to remove the final character.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Here is the solution, someone might use the idea later:



@Rob: Thanks for directions!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

You're welcome
 
 
subject: JTextField + JTextArea -> Problem with backspace-ing
 
Similar Threads
JTextArea
how to take and check input in Java
Grouping text in JTextArea
Creating a command prompt is swing
KeyListener that listens everywhere inside a JDialog