954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Undo

Hi everyone,

I am trying to add an undo listener to a jtable but could not get it to work. Does anyone know how to add a undo function to a jtable assuming the default cell editor component is a jtextfield?

A small code sample would be helpful

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

are you wanting to add it to the textfield? If so, all you have to do is create a document from the textfield..

import these:

import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.text.*;


add an UndoableEditListener

create an UndoManager as an instance variable

create a Document as an instance variable

then create the document and add the listener to it(That is if you want to add it to a textfield,this is the way I took it)

doc = (Document)txtDisplay.getDocument();
		doc.addUndoableEditListener(this);


undoable edit event stuff

public void undoableEditHappened(UndoableEditEvent uee)
	{
		undoManager.addEdit(uee.getEdit());
   	}


then do the undo and redo like this:

//don't need this method
	public void processUndo()
	{
		 try
		 {
			 /* call the UndoManagers undo method
			  */
			 undoManager.undo();
		 }
		 catch (CannotUndoException cre)
		 {
		 }

	}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Hi everyone,

I already tried that but it does not seem to work for the jtable as i feel it has something to to with the cell renderer

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You