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

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)
		 {
		 }

	}

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

This article has been dead for over six months. Start a new discussion instead.