-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCompoundUndoManagerDemo.java
More file actions
69 lines (57 loc) · 1.99 KB
/
CompoundUndoManagerDemo.java
File metadata and controls
69 lines (57 loc) · 1.99 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;
public class CompoundUndoManagerDemo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
// Create a simple toolbar
JToolBar toolBar = new JToolBar();
toolBar.add( createButtonFromAction( new StyledEditorKit.BoldAction() ) );
toolBar.add( createButtonFromAction( new StyledEditorKit.ItalicAction() ) );
toolBar.add( createButtonFromAction( new StyledEditorKit.UnderlineAction() ) );
// Create a text pane
final JTextPane textPane = new JTextPane();
textPane.setPreferredSize( new Dimension(200, 200) );
String text =
"public class SomeClass\n" +
"{\n\t// Enter code here\n}";
textPane.setText(text);
CompoundUndoManager undoManager = new CompoundUndoManager( textPane );
// Create undo/redo buttons
JPanel buttons = new JPanel();
JButton undo = new JButton(undoManager.getUndoAction());
buttons.add( undo );
JButton redo = new JButton(undoManager.getRedoAction());
buttons.add( redo );
// Add components to the frame
JPanel main = new JPanel();
main.setLayout( new BorderLayout() );
main.add(toolBar, BorderLayout.NORTH);
main.add( new JScrollPane(textPane), BorderLayout.CENTER );
main.add(buttons, BorderLayout.SOUTH);
JFrame frame = new JFrame("Compound Undo Manager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(main);
frame.setSize(450, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JButton createButtonFromAction(Action action)
{
JButton button = new JButton( action );
button.setRequestFocusEnabled(false);
return button;
}
}