Highlighting and Highlighters

Java > Core SWING advanced programming > 9. THE SWING UNDO PACKAGE

 

Chapter 9. THE SWING UNDO PACKAGE

Topics in this Chapter

  • Using the Undo Package

  • Creating Your Own UndoableEdits

  • Extending UndoManager

A graphical user interface (GUI) should be designed to allow the user to do what he or she needs to do with minimal effort. There is, however, another side to this coin. As well as making it possible for the user to do something, it is also important to give the user the ability, where it makes sense, to reverse an action that turned out to be inappropriate. For example, as we saw in the first few chapters of this book, the Swing text components allow the user to change the style of large stretches of text or to delete anything from a single character to entire paragraphs with only a few mouse clicks or key presses. It is not so obvious, however, what the users should do when they realize that the wrong piece of text was deleted or the heading that had just been underlined should have been left as it was.

Fortunately, Swing contains a package designed to make it possible for you to implement your application in such a way as to make it possible for the user to reverse actions that the developer considers reversible. The undo feature (which resides in the javax.swing.undo package) consists of a small set of interfaces, classes, and events that you can use to build your application in such a way that it is possible for one change or a group of several changes to be stored so that they can be undone, either individually or together, on request from the user. This support is not free, however. To make use of it, you have to structure the actions that you take in response to button presses or the activation of menu items so that the effects of these actions can be stored and later reversed. The undo package provides the mechanism for expressing an undoable action, but it does not do all the work for you.

This chapter looks in detail at the Swing "undo" package. Although this package is a free-standing piece of software that can be used in many different contexts, some of its most obvious applications are within the realms of text processing and, indeed, the Swing text components make use of it to provide the basis for incorporating undo functionality within your applications. As well as looking at how the text components use the undo mechanism, you'll also see how to make direct use of it by enhancing it to improve your user interface.

 

 



Core Swing
Core Swing: Advanced Programming
ISBN: 0130832928
EAN: 2147483647
Year: 1999
Pages: 55
Authors: Kim Topley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net