Java Reference
In-Depth Information
55.
display.setText(text);
56.
}
57.
58.
// Method to enable the label
59.
public void
enableTest(){
60.
testItem.setEnabled(
true
);
61.
}
62.
63.
// Method to disable the label
64.
public void
disableTest(){
65.
testItem.setEnabled(
false
);
66.
}
67.
}
File:
its/Menus/MenuDriver.java
1.
package
its.Menus;
2.
3.
public class
MenuDriver {
public static void
main(String[] args) {
4.
MenuFrame mf =
new
MenuFrame();
5.
mf.showIt();
6.
}
7.
}
8.
■
8.3
The control part
The control part is realized by an action listener. We implement an
Action-
Listener
in class
MenuListener
.Asareaction to events coming from the menu
items the
MenuListener
has to update the information displayed in the
MenuFrame
and to enable or disable the 'Test' item. In order to have access to the frame the
MenuListener
receives a reference to a
MenuFrame
in the constructor.
As mentioned above, menu items behave much like buttons. If a menu item
has a listener assigned to it and is clicked, the runtime system generates an object
of type
ActionEvent
. This contains information on the type of event, e.g. which
menu item was clicked. The runtime system calls method
actionPerformed
of
the listener.
In our example, method
actionPerformed
exploits the action command to find
out which button was clicked using nested
if-then-else
statements. Depending
on the action command, a specific action is performed, namely the name of the
selected menu item is displayed in the label. Below is the code listing for this class.
Figure 8.2 shows the result of running the application.
Search WWH ::
Custom Search