Java Reference
In-Depth Information
6.3.1
Examining some dangerous practices
The program shown in listing 6.2 was created with VisualAge for Java. It's a
simple applet called TestView. Notice that the development environment reg-
istered a method to handle the button action without a corresponding
remove method.
Listing 6.2
Dangerously registering an action without a corresponding remove
package memory;
public class TestView extends java.applet.Applet {
class IvjEventHandler implements java.awt.event.ActionListener {
Trigger
for an
event
public void actionPerformed(java.awt.event.ActionEvent e) {
if (e.getSource() == TestView.this.getTest())
connEtoM1(e);
};
};
IvjEventHandler ivjEventHandler = new IvjEventHandler();
private java.awt.Button ivjTest = null;
private java.awt.TextField ivjTextField = null;
public TestView() {
super();
}
o Event
handler
class
o Method is
fired when
the event
handler
gets called.
private void connEtoM1(java.awt.event.ActionEvent arg1) {
try {
getTextField().setText("After");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
private java.awt.Button getTest() {
if (ivjTest == null) {
try {
ivjTest = new java.awt.Button();
ivjTest.setName("Test");
ivjTest.setBounds(117, 166, 56, 23);
ivjTest.setLabel("Test");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
return ivjTest;
}
private java.awt.TextField getTextField() {
if (ivjTextField == null) {
try {
Search WWH ::




Custom Search