*
The moose likes Swing / AWT / SWT and the fly likes Undo/Redo with JTextArea? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Undo/Redo with JTextArea?" Watch "Undo/Redo with JTextArea?" New topic
Author

Undo/Redo with JTextArea?

Steve Morrison
Greenhorn

Joined: Oct 30, 2003
Posts: 6
I'm using code from here: http://www.java2s.com/Code/Java/Swing-JFC/CreatingTextAreawithUndoRedoCapabilities.htm modified a bit, and am getting splendid results undoing and redoing to my heart's content. HOWEVER, when I load a new file up into the text area, my undo and redo cease to function.
Any suggestions as to where this is going screwy would be wonderful. Here is the code to load the file
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
HOWEVER, when I load a new file up into the text area, my undo and redo cease to function.


The code you are using appears to add a listener to the Document.

I would guess that when you read text from a file a new Document is created so you would need to add the listener to the new Document.
Steve Morrison
Greenhorn

Joined: Oct 30, 2003
Posts: 6
Thanks. I hope I didn't take the easy (lazy?) way out, but I switched methods of reading the text in, which resolves my issue. At least, as far as I can tell. Without being able to see the rest of the code, are there any immediate drawbacks that would come to mind?

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
How is that the easy way?

1. Concatenating Strings like that is always a bad idea.

2. Adding the listener to the document is one line of code. Why is that so difficult?

The perceived easy way usually results in more difficulty in the long run. Maybe not in this example but when you try to use the same approach in a different project.
Steve Morrison
Greenhorn

Joined: Oct 30, 2003
Posts: 6
Rob Camick wrote:How is that the easy way?

1. Concatenating Strings like that is always a bad idea.

2. Adding the listener to the document is one line of code. Why is that so difficult?

The perceived easy way usually results in more difficulty in the long run. Maybe not in this example but when you try to use the same approach in a different project.


Well, when you put it like that ;)

The BR is easier in that I know it to work. Better is a completely different matter I see.
I have successfully added the listener to the original code, so the undo/redo is all fine and dandy following the update.

Now, I have another question (sorry!).
In the code, there is another JTextArea object used to count lines in the main text area. Again, it works great before a saved file is opened.

The code to initialize the listener for this is

I'm at at a loss when trying to reimplement this after a file is loaded.

Please forgive my ignorance. I'm quite rusty and this is not my code that I'm altering so I'm working at trying to understand it all.
 
Don't get me started about those stupid light bulbs.
 
subject: Undo/Redo with JTextArea?
 
Similar Threads
What is the difference between implementing undo/redo using momento and command pattern.
How to implment Undo/Redo for a editor
How to Save from JTextArea using JFileChooser as a TXT file.
Double Duty FileChooser
JFileChoosers and file extensions