Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undo/Redo Problems

Status
Not open for further replies.

Yorkiee

Technical User
Oct 2, 2003
18
0
0
IE
Could somebody throw some light on this particular code. It's throwing up some strange errors, ie: class or interface expected, that kind of thing. so it's probably just a mattter of having the parts in the right areas

Code:
jTextArea1 jtxt = new jTextArea1();
final UndoManager undo = new UndoManager();
        Document doc = textarea.getDocument();
        doc.addUndoableEditListener(new UndoableEditListener() {
         public void undoableEditHappened(UndoableEditEvent evt) {
             undo.addEdit(evt.getEdit());
         }
     });

     // Create an undo action and add it to the text component
     textcomp.getActionMap().put("Undo",
         new AbstractAction("Undo") {
             public void jButton18_actionPerformed(ActionEvent evt) {
                 try {
                     if (undo.canUndo()) {
                         undo.undo();
                     }
                 } catch (CannotUndoException e) {
                 }
             }
        });



     // Create a redo action and add it to the text component
     textarea.getActionMap().put("Redo",
         new AbstractAction("Redo") {
             public void jButton19_actionPerformed(ActionEvent evt) {
                 try {
                     if (undo.canRedo()) {
                         undo.redo();
                     }
                 } catch (CannotRedoException e) {
                 }
             }
         });*/
 
// your code check if the program can undo or not, if it can, it undo or redo.
// The following is program that work from
Code:
// save this code as UndoableTextArea.java 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.undo.*; 

/** 
* UndoableTextArea.java 
* This is the TextArea with Undo and Redo Capabilities 
* 
* @author Rahul Sapkal(rahul@javareference.com) 
*/ 
public class UndoableTextArea extends JTextArea implements UndoableEditListener, FocusListener, KeyListener 
{ 
    //Setting Undo Limit to 1500 edits 
    public static final int UNDO_LIMIT = 1500; 

    //UndoManager 
    private UndoManager m_undoManager; 

    //constructor 
    public UndoableTextArea() 
    { 
        this(new String()); 
    } 
     
    //constructor 
    public UndoableTextArea(String text) 
    { 
        super(text); 
     
        //add the UndoableEditListener to the TextArea 
        getDocument().addUndoableEditListener(this); 
        //add the KeyListener 
        this.addKeyListener(this); 
        //add the FocusListener 
        this.addFocusListener(this); 
    } 

    //createUndoMananger creating undo manager 
    private void createUndoMananger() 
    { 
        m_undoManager = new UndoManager(); 
        m_undoManager.setLimit(UNDO_LIMIT); 
    } 
     
    //removeUndoMananger removing undo manager 
    private void removeUndoMananger() 
    { 
        m_undoManager.end(); 
    } 
         
    public void focusGained(FocusEvent fe) 
    { 
        //creating undo manager on focus gained 
        createUndoMananger();         
    } 

    public void focusLost(FocusEvent fe) 
    { 
        //removing undo manager on focus lose 
        removeUndoMananger(); 
    } 

    //undoableEditHappened called when edit happened 
    public void undoableEditHappened(UndoableEditEvent e) 
    { 
        //add the edits to the unod manager 
        m_undoManager.addEdit(e.getEdit()); 
    } 
     
    public void keyPressed(KeyEvent e)  
    { 
        //identify the key pressed is the combination of 
        //Controlkey and Z key 
        if((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown())) 
        {                 
            try 
            { 
                //Undo changes 
                m_undoManager.undo(); 
            } 
            catch(CannotUndoException cue) 
            { 
                Toolkit.getDefaultToolkit().beep(); 
            } 
        } 
         
        //identify the key pressed is the combination of 
        //Controlkey and Y key 
        if((e.getKeyCode() == KeyEvent.VK_Y) && (e.isControlDown())) 
        {                 
            try 
            { 
                //Redo changes 
                m_undoManager.redo(); 
            } 
            catch(CannotRedoException cue) 
            { 
                Toolkit.getDefaultToolkit().beep(); 
            } 
        } 
    } 

    public void keyReleased(KeyEvent e)  
    { 
    } 
     
    public void keyTyped(KeyEvent e) 
    { 
    } 
}
Code:
// save this code as TestFrame.java
import javax.swing.*; 
import javax.swing.text.*; 
import java.awt.*; 
import java.awt.event.*; 

/** 
* TestFrame.java 
* This test frame is to demonstrates the UndoableTextArea 
* 
* @author Rahul Sapkal(rahul@javareference.com) 
*/ 
public class TestFrame extends JFrame 
{ 
    UndoableTextArea m_undoableTextArea; 
     
    public TestFrame() 
    { 
        super("Undoable TextArea "); 
         
        m_undoableTextArea = new UndoableTextArea(); 
         
        JScrollPane sc = new JScrollPane(m_undoableTextArea); 
         
        getContentPane().setLayout(new BorderLayout(10, 10)); 
         
        getContentPane().add(BorderLayout.NORTH, new JLabel("Press, CTRL+Z to Undo, CTRL+Y to Redo...")); 
        getContentPane().add(BorderLayout.CENTER, sc); 
    } 

    public static void main(String[] arg) 
    { 
        TestFrame m = new TestFrame(); 
         
        m.setVisible(true); 
        m.setSize(new Dimension(400, 300)); 
        m.validate(); 
    } 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top