The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Undoable edit events occur when an operation that can be undone occurs on a component. Currently, only text components fire undoable edit events, and then only indirectly. The text component's document fires the events. For text components, undoable operations include inserting characters, deleting characters, and modifying the style of text. Programs typically listen to undoable edit events to assist in the implementation of undo and redo commands.
Here is the undoable edit event handling code from an application called TextComponentDemo
.
... //where initialization occurs document.addUndoableEditListener(new MyUndoableEditListener()); ... protected class MyUndoableEditListener implements UndoableEditListener { public void undoableEditHappened(UndoableEditEvent e) { //Remember the edit and update the menus undo.addEdit(e.getEdit()); undoAction.updateUndoState(); redoAction.updateRedoState(); } }
You can find a link to the source file for TextComponentDemo
in the example index for Using Swing Components. For a discussion about the undoable edit listener aspect of the program see Implementing Undo and Redo
The UndoableEditListener Interface
Because UndoableEditListener
has only one method, it has no corresponding adapter class.
Method | Purpose |
---|---|
undoableEditHappened(UndoableEditEvent) | Called when an undoable event occurs on the listened-to component. |
Method | Purpose |
---|---|
Object getSource() (in java.util.EventObject ) |
Return the object that fired the event. |
UndoableEdit getEdit() | Returns an UndoableEdit object that represents the edit that occurred and contains information about and commands for undoing or redoing the edit. |
The following table lists the examples that use undoable edit listeners.
Example | Where Described | Notes |
---|---|---|
TextComponentDemo |
Implementing Undo and Redo | Implements undo and redo on a text pane with help from an undoable edit listener. |