aspose file tools*
The moose likes Beginning Java and the fly likes remove lines in a JtextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "remove lines in a JtextArea" Watch "remove lines in a JtextArea" New topic
Author

remove lines in a JtextArea

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
whenever Push is used the item that was entered is added to the JtextArea for stack. When the pop button is pushed the item is removed from the stack but it isn't removed from the stack text area. Is there a way to remove the item added last to the stack in the Jtextarea?
[edit]Newlines added: text too wide for screen. CR[/edit]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

First you get the text from the JTextArea. (You already know how to do that.) Then you remove the last entry you wrote to the so-called "stack" from that text. Finally you write the resulting text, after you did the removal, back to the JTextArea (which you also already know how to do).

I have to say that JTextArea is not really the best choice for a stack, but perhaps this is some kind of assignment and you can't choose something more suitable.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I would like to hear if there is a better way to do this
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

If you're asking whether there's a better choice of component than JTextArea for a stack, then I would say that a JList would be a better choice.

But if you're asking whether there's a better way to pop the last entry of your stack-which-is-really-a-string, then there isn't really. Just find the last entry and truncate the string before it.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I ended up just changing my toString to go through my stack and iterate through and add each item to it can print the entire thing but thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
. . . there are simple ways to catenate Strings (adding them together) but there aren't such easy ways to subtract Strings. Agree about JList; another possibility is to use a StringBuilder to create the text and renew the text every time you pop from your stack.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: remove lines in a JtextArea