This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 19679
    
  18

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: 19679
    
  18

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField + JTextArea -> Problem with backspace-ing