I thought I knew how to do this, and I do seem to have this working fine for cut/copy/paste/select all keybindings CTRL X/C/V/A respectively.
However I tried to do this for CTRL X and Y undo/redo and it's not working as I intended. Can anyone see what I'm doing wrong?
I created a SSCCE based off the original example I was following from: [http://www.java2s.com/Code/Java/Swing-JFC/Undoredotextarea.htm]
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;
public class UndoRedoTextArea extends JFrame implements KeyListener{
private static final long serialVersionUID = 1L;
protected JTextArea textArea = new JTextArea();
protected UndoManager undoManager = new UndoManager();
protected JButton undoButton = new JButton("Undo");
protected JButton redoButton = new JButton("Redo");
public UndoRedoTextArea() {
super("Undo/Redo Demo");
undoButton.setEnabled(false);
redoButton.setEnabled(false);
JPanel buttonPanel = new JPanel(new GridLayout());
buttonPanel.add(undoButton);
buttonPanel.add(redoButton);
JScrollPane scroller = new JScrollPane(textArea);
getContentPane().add(buttonPanel, BorderLayout.NORTH);
getContentPane().add(scroller, BorderLayout.CENTER);
textArea.getDocument().addUndoableEditListener(
new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
updateButtons();
}
});
undoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
undoManager.undo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
updateButtons();
}
});
undoButton.addKeyListener(this);
redoButton.addKeyListener(this);
redoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
undoManager.redo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
updateButtons();
}
});
setSize(400, 300);
setVisible(true);
}
public void updateButtons() {
undoButton.setText(undoManager.getUndoPresentationName());
redoButton.setText(undoManager.getRedoPresentationName());
undoButton.setEnabled(undoManager.canUndo());
redoButton.setEnabled(undoManager.canRedo());
}
public static void main(String argv[]) {
new UndoRedoTextArea();
}
public void keyPressed(KeyEvent e){
if (e.equals(KeyStroke.getKeyStroke
(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK))){
// undo
try {
undoManager.undo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
updateButtons();
}
else if (e.equals(KeyStroke.getKeyStroke
(KeyEvent.VK_Y, InputEvent.CTRL_DOWN_MASK))){
// redo
try {
undoManager.redo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
updateButtons();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
Edited by: G-Unit on Oct 24, 2010 5:30 AM