CopyPastor

Detecting plagiarism made easy.

Score: 0.8089205622673035; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2024-03-22
by Jörg

Original Post

Original - Posted on 2012-08-20
by MadProgrammer



            
Present in both answers; Present only in the new answer; Present only in the old answer;

An update to the UndoableJTextField class to avoid some compiler warnings (jdk21). ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.undo.*;
public class UndoableTextField extends JTextField { KeyStroke undoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK); KeyStroke redoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_DOWN_MASK);
public UndoableTextField() { UndoManager undoManager = new UndoManager(); getDocument().addUndoableEditListener(new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { undoManager.addEdit(e.getEdit()); } });
getInputMap().put(undoKeyStroke, "undoKeyStroke"); getActionMap().put("undoKeyStroke", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { try { undoManager.undo(); } catch (CannotUndoException cue) { } } });
getInputMap().put(redoKeyStroke, "redoKeyStroke"); getActionMap().put("redoKeyStroke", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { try { undoManager.redo(); } catch (CannotRedoException cre) { } } }); } } ```
From you're example, it's difficult to know how much you've done, but I was able to get this to work...
private UndoManager undoManager;
// In the constructor
undoManager = new UndoManager(); Document doc = textArea.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) {
System.out.println("Add edit"); undoManager.addEdit(e.getEdit());
} });
InputMap im = textArea.getInputMap(JComponent.WHEN_FOCUSED); ActionMap am = textArea.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Undo"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Redo");
am.put("Undo", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { try { if (undoManager.canUndo()) { undoManager.undo(); } } catch (CannotUndoException exp) { exp.printStackTrace(); } } }); am.put("Redo", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { try { if (undoManager.canRedo()) { undoManager.redo(); } } catch (CannotUndoException exp) { exp.printStackTrace(); } } });

        
Present in both answers; Present only in the new answer; Present only in the old answer;