Java Reference
In-Depth Information
Figure 21-5. The undoable drawing panel at work
The main program in Listing 21-4 looks practically identical to the earlier example of
supporting undo and redo operations in a Swing text component, in Listing 21-2. You simply
need to create an UndoManager to manage the undoable operations and associate it to the undo-
able object. The usage of the Undo Framework works the same here, except that the undoable
object is the yet-to-be-created UndoableDrawingPanel class (Listing 21-5).
Listing 21-4. The UndoDrawing Test Driver
import javax.swing.*;
import javax.swing.undo.*;
import java.awt.*;
public class UndoDrawing {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Drawing Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UndoableDrawingPanel drawingPanel = new UndoableDrawingPanel();
UndoManager manager = new UndoManager();
drawingPanel.addUndoableEditListener(manager);
JToolBar toolbar = new JToolBar();
JButton undoButton =
new JButton(UndoManagerHelper.getUndoAction(manager));
toolbar.add(undoButton);
JButton redoButton =
new JButton(UndoManagerHelper.getRedoAction(manager));
toolbar.add(redoButton);
Search WWH ::




Custom Search