1 Replies - 225 Views - Last Post: 11 November 2017 - 03:00 PM Rate Topic: -----

#1 izic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 223
  • Joined: 15-January 09

Setting keystroke key for Undo/Redo Action JMenu Item

Posted 10 November 2017 - 03:33 PM

I've been trying to set the keyboard keys for the undo and redo options in my JMenu. I was going to try doing so using the
setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); 
code but then I realized that this only works for actual JMenu Items and not UndoAction and RedoAction.

I also tried this code:

KeyStroke undoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.META_MASK);
KeyStroke redoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.META_MASK);

undoAction = new UndoAction();
jTextPane.getInputMap().put(undoKeystroke, "undoKeystroke");
jTextPane.getActionMap().put("undoKeystroke", undoAction);

redoAction = new RedoAction();
jTextPane.getInputMap().put(redoKeystroke, "redoKeystroke");
jTextPane.getActionMap().put("redoKeystroke", redoAction);


But it did not work for me. Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Setting keystroke key for Undo/Redo Action JMenu Item

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 19,226
  • Joined: 14-September 07

Re: Setting keystroke key for Undo/Redo Action JMenu Item

Posted 11 November 2017 - 03:00 PM

I'm assuming you got that snippet from here, which is one of the top results when googling for this issue.

I'm not sure what else you have going on code wise (it's always helpful to post a compilable/runnable sample when asking for assistance), but all you need to do here is set an undo listener on the text pane and tie it to crtl+key press.

I am borrowing the UndoHandlers from the first link and a barebones crtl+z/crtl+y would look something like this:

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Event;

class Test extends JFrame {

protected UndoHandler undoHandler = new UndoHandler();
protected UndoManager undoManager = new UndoManager();

class UndoHandler implements UndoableEditListener
{

  public void undoableEditHappened(UndoableEditEvent e)
  {
    undoManager.addEdit(e.getEdit());
    undoAction.update();
    redoAction.update();
  }
}

class UndoAction extends AbstractAction
{
  public UndoAction()
  {
    super("Undo");
    setEnabled(false);
  }

  public void actionPerformed(ActionEvent e)
  {
    try
    {
      undoManager.undo();
    }
    catch (CannotUndoException ex)
    {
      // TODO deal with this
      //ex.printStackTrace();
    }
    update();
    redoAction.update();
  }

  protected void update()
  {
    if (undoManager.canUndo())
    {
      setEnabled(true);
      putValue(Action.NAME, undoManager.getUndoPresentationName());
    }
    else
    {
      setEnabled(false);
      putValue(Action.NAME, "Undo");
    }
  }
}

class RedoAction extends AbstractAction
{
  public RedoAction()
  {
    super("Redo");
    setEnabled(false);
  }

  public void actionPerformed(ActionEvent e)
  {
    try
    {
      undoManager.redo();
    }
    catch (CannotRedoException ex)
    {
      // TODO deal with this
      ex.printStackTrace();
    }
    update();
    undoAction.update();
  }

  protected void update()
  {
    if (undoManager.canRedo())
    {
      setEnabled(true);
      putValue(Action.NAME, undoManager.getRedoPresentationName());
    }
    else
    {
      setEnabled(false);
      putValue(Action.NAME, "Redo");
    }
  }
}

  private UndoAction undoAction;
  private RedoAction redoAction;
  private JTextPane jTextPane;

  Test(){
    super();
    setSize(500,500);
    JMenu editMenu = new JMenu("Edit");
    KeyStroke undoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
    KeyStroke redoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);

    jTextPane = new JTextPane();
    Document editorPaneDocument = jTextPane.getDocument();
    editorPanedocument.addUndoableEditListener(undoHandler);

    undoAction = new UndoAction();
    jTextPane.getInputMap().put(undoKeystroke, "undoKeystroke");
    jTextPane.getActionMap().put("undoKeystroke", undoAction);

    redoAction = new RedoAction();
    jTextPane.getInputMap().put(redoKeystroke, "redoKeystroke");
    jTextPane.getActionMap().put("redoKeystroke", redoAction);
    JMenuItem undoMenuItem = new JMenuItem(undoAction);
    JMenuItem redoMenuItem = new JMenuItem(redoAction);
    editMenu.add(undoMenuItem);
    editMenu.add(redoMenuItem);
    editMenu.setVisible(true);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(editMenu);
    setJMenuBar(menuBar);
    add(jTextPane);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }


  public static void main(String[] args){
    new Test();
  }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1