Java Reference
In-Depth Information
Dialogues
12
In this chapter we introduce dialogues as a further means for user-program inter-
action. They are used when the application needs information from the user in
order to proceed. First, we look at dialogues that are predefined in Swing and
then turn to user-defined dialogues. We shall do this by extending the examples
from previous chapters into a small editor. We will also learn how to transfer data
between dialogues and the application.
In Chapter 10 we read a fixed file into our editor. In Section 11.1 we saw how
to display the text so that it can be scrolled. The use of menus was presented
in Chapter 8. Here, we combine these three things into an editor application. In
addition to that, we add some dialogues for choosing a file, searching and showing a
warning message. We begin by developing the skeleton of the application to which
we then add more and more functions. No model is used for the actual data -
the text - because our focus is on dialogues. Text models will be considered in
Chapter 18.
12.1
The basic editor application
Our editor should support the following operations. There should be menus where
the user can select the actions 'Load', 'Save' and 'Search'. The first one allows the
user to choose the text file to be loaded. The second one saves the currently loaded
text file (under the same name). The third action allows the user to search for a
word in the text. In order to implement these features, dialogues will be used.
Before turning to dialogues we first set up the skeleton of the editor applications
and fill in the relevant parts in later sections. The mechanism for editing a loaded
text is automatically supplied by the Swing text component used.
In the following listing of class EditorSkeletonFrame we define a frame with a
JEditorPane as a central component. The frame has a menu bar with two menus,
'File' and 'Tools'. The first menu has three items 'Load', 'Save' and 'Exit'. The
second menu has only one item 'Search'. The implementation is a combination of
the implementations from Chapter 10, Section 11.1 and Chapter 8.
The skeleton contains four methods that implement the reactions to select-
ing a menu item. For now, the only action is to print a message on the console.
Search WWH ::




Custom Search