Java Reference
In-Depth Information
In larger applications, the user interface might become overloaded with buttons and other widgets for
invoking different program actions. In order to minimize the space that is occupied by those widgets,
pull-down menus can be used instead, especially for less important actions or configuration options.
This approach gives you the opportunity to add many menu items to a menu without wasting the scarce
screen space of a mobile device. An AWT menu consists of at least three classes:
• One
MenuBar
containing a set of
Menu
s
• Some
Menu
s consisting of multiple
Menuitem
s
•
MenuItem
s sending
ActionEvent
s to registered
Listener
s
This coherence is illustrated in
Figure 4.6
.
Figure 4.6. A
MenuBar
consisting of two
Menu
s where the File menu holds some
MenuItem
s.
Another opportunity to save screen space is to move widgets to dialogs.
Dialog
is a class similar to
Frame
. Both classes are direct subclasses of the
Window
class. Like a frame, a dialog represents a
rectangular area of the screen and can hold a set of widgets. In contrast to frames, dialogs can be modal
and are designed as a temporary display for obtaining user input or similar purposes. A dialog needs to
have a frame as a parent.
In order to show how menus and dialogs can be used in a PDAP application, we create a small
shopping chart application, showing a list consisting of Amount and Item columns (see
Listing 4.5
)
.
For displaying the list, we use a
Panel
with
GridLayout
. In order to get as much space as possible
for the user data, we create a separate dialog class for adding new rows,
InsertItemDialog
. The
dialog is invoked by selecting the menu item Insert of the Item menu. As already illustrated in
Figure
creates a
MenuBar
, registers the
MenuItem
insert with the
ActionListener
of the
Frame
, and
finally concatenates the
MenuBar
with the frame:
MenuBar menuBar = new MenuBar();
Menu menu = new Menu ("Items");
MenuItem insertItem = new MenuItem ("insert");
insertItem.addActionListener(this);
menu.add(insertItem);
menuBar.add(menu);
frame.setMenuBar (menuBar);
Search WWH ::
Custom Search