-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUndoManager_JavaTutorial.java
More file actions
84 lines (81 loc) · 3.15 KB
/
Copy pathUndoManager_JavaTutorial.java
File metadata and controls
84 lines (81 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;
/**
*
* @author Vincent
*/
public class UndoManager_JavaTutorial {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
/Component Setup/
JFrame window = new JFrame("Java Tutorial: UndoManager");
final JTextArea area_1 = new JTextArea();
JMenuItem redoEdit = new JMenuItem("Redo");
JMenuItem undoEdit = new JMenuItem("Undo");
final JScrollPane scroll = new JScrollPane(area_1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
UndoManager editManager = new UndoManager();
/Component Functions/
area_1.setLineWrap(true);
area_1.setWrapStyleWord(true);
area_1.getDocument().addUndoableEditListener(
new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
editManager.addEdit(e.getEdit());
}
});
area_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
if (editManager.canRedo()) {
redoEdit.setEnabled(true);
} else {
redoEdit.setEnabled(false);
}
if (editManager.canUndo()) {
undoEdit.setEnabled(true);
} else {
undoEdit.setEnabled(false);
}
JPopupMenu popup = new JPopupMenu();
popup.add(undoEdit);
popup.add(redoEdit);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
redoEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editManager.canRedo()) {
editManager.redo();
}
}
});
undoEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editManager.canUndo()) {
editManager.undo();
}
}
});
window.add(area_1);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 500);
window.setVisible(true);
window.setLocationRelativeTo(null);
}
}