Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Undo/Redo with JTextArea?

 
Steve Morrison
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic