Results 1 to 2 of 2
  1. #1
    geek is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Re: Undo in swing

    I am having some troubles implementing undo. it seems to be undoing and redoing single character deletes. but it is not undoing/redoing entire word deletes. I have implemented my own function of implementing Ctrl+Backspace which deletes the left word. However if you delete a word using Ctrl+Bkspc and then hit Ctrl+Z, first the whole text component gets blanked out and then when you press Ctrl+Z again, then the text reappears with the cut word reappearing as it should have done with the single C+Z. Cannot figure out why this is happening. This is my first post here. So please pardon any beginner mistakes.

    I have attached the code below for reference. Any help is deeply appreciated.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.KeyStroke;
    import javax.swing.event.UndoableEditEvent;
    import javax.swing.event.UndoableEditListener;
    import javax.swing.text.Document;
    import javax.swing.undo.CannotRedoException;
    import javax.swing.undo.CannotUndoException;
    import javax.swing.undo.UndoManager;
    public class cutClass {
      public static void main(String[] a) {
        String ACTION_KEY = "theAction";
        JFrame frame = new JFrame("KeyStroke Sample");
        JTextField textField = new JTextField(20);
        frame.setSize(400, 200);
    	static String getDisplayString (JTextField source){
    		return source.getText();
    	static void setDisplayString(JTextField source,String s){
    	static void deleteLeftWord(JTextField ruleEditorArea){
    	    String ACTION_KEY = "deleteLeft";	    
    	    AbstractAction actionListener = new AbstractAction() {
    	      public void actionPerformed(ActionEvent actionEvent) {
    	    	  JTextField source = (JTextField) actionEvent.getSource();
    	    	  if (getDisplayString(source).lastIndexOf(" ") > 0)
    	    		  setDisplayString(source, (getDisplayString(source).substring(0,getDisplayString(source).lastIndexOf(" "))  ));
    	    		  setDisplayString(source, "");
    	    KeyStroke deleteWord = KeyStroke.getKeyStroke("control BACK_SPACE");
    	    InputMap inputMap = ruleEditorArea.getInputMap();
    	    inputMap.put(deleteWord, ACTION_KEY);
    	    ActionMap actionMap = ruleEditorArea.getActionMap();
    	    actionMap.put(ACTION_KEY, actionListener);
    	static void undoAction(JTextField textField){
    		final UndoManager undo = new UndoManager();
    		//final RedoManager redo = new RedoManager();
    	    Document doc = textField.getDocument();
    	    doc.addUndoableEditListener(new UndoableEditListener() {
    	      public void undoableEditHappened(UndoableEditEvent evt) {
    	    textField.getActionMap().put("Undo", new AbstractAction("Undo") {
    	      public void actionPerformed(ActionEvent evt) {
    	        try {
    	          if (undo.canUndo()) {
    	        	System.out.println("CAN UNDO");
    	        } catch (CannotUndoException e) {
    	    textField.getActionMap().put("Redo", new AbstractAction("Redo") {
    		      public void actionPerformed(ActionEvent evt) {
    		        try {
    		          if (undo.canRedo()) {
    		            System.out.println("CAN REDO");
    		        	System.out.println("CANT REDO");
    		        } catch (CannotRedoException e) {
    	    textField.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
    	    textField.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
    Last edited by Fubarable; 11-30-2011 at 12:30 AM. Reason: code tags added

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

Similar Threads

  1. add an undo listener to a Jtable
    By christina in forum Advanced Java
    Replies: 12
    Last Post: 01-29-2009, 08:47 AM
  2. SWT Undo Redo
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:43 PM
  3. Undo/Redo JTextArea
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:52 PM
  4. Undo shortcut malfunctioning.
    By Eranga in forum NetBeans
    Replies: 0
    Last Post: 03-31-2008, 06:12 AM
  5. undo java
    By new214 in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 10:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts