File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes awt  event handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "awt  event handling" Watch "awt  event handling" New topic
Author

awt event handling

ravi ckumar
Greenhorn

Joined: Dec 08, 2000
Posts: 18
A component subclass that has executed enableEvents() to enable processing of a certain kind of event cannot also use an adapter as a listener for the same kind of event.
A. True
B. False
I feel that the answer to the above is false but in phillip heller book it was given as true, my reason is when u are using the enableEvents mechanism u r already extendimg to the component like button etx how can u again extend to any other adapter class as java does not allow multiple inheritance.??
Also can any body tell me the site where the phillip heller book errata is availabe(pls i need the url)
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi,
I guess the answer is correct. You can use the adapter classes with enableEvents. It is true that you cannot subclass Adapter class when you use enableEvents since Java doesn't support multiple inheritance. But you can do it the following way.
<code><pre>
import java.awt.*;
import java.awt.event.*;
class demoAdapter extends Frame
{
demoAdapter()
{
myButton btn=new myButton("Click Me!");
add(btn);
}
public static void main(String args[])
{
demoAdapter frameobj=new demoAdapter();
frameobj.setSize(50,50);
frameobj.setVisible(true);
}

class myButton extends Button
{
public myButton(String label)
{
super(label);
enableEvents(AWTEvent.KEY_EVENT_MASK);
addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
System.out.println("Listener Invoked");
}
});
}
public void processKeyEvent(KeyEvent e)
{
System.out.println("processKeyEvent for myButton called");
super.processKeyEvent(e);
}
}
}
</pre></code>
In the above case, you can use an adapter class for the listener object and this is a case wherein you can use both enableEvents and Adapter class together.
Hope it answers your query.
Regds,
Aparna
Scott Kiel
Greenhorn

Joined: Jan 05, 2001
Posts: 15
My RHE edition gives the answer as false - a component may have an unlimited number of listeners (including adapter subclasses) whether or not it has explicitly called enableEvents().
ravi ckumar
Greenhorn

Joined: Dec 08, 2000
Posts: 18
the explanation u gave seems to be right , but what is the purpose of adding a listener , i mean u either go for adding listener or u use enable events, what is the purpose of using both , even if we add both can u pls explain how the flow goes i mean will the enable events method be invoked or the keytyped method be invoked because of adding the listener.
As far as the question is concerned i feel now the ans has to be taken as true as we can use both.
Mike Cramer
Greenhorn

Joined: Jan 10, 2001
Posts: 2
There is one line in the example Aparna gave:
super.processKeyEvent(e);
My Complete Java 2 Cert Study Guide says without this line, the action listerner won't be triggered, but I took it out (from a similar program), it sitll works.
Any comment won't be appreciate.
Mike Cramer
Greenhorn

Joined: Jan 10, 2001
Posts: 2
Sorry, I meant Any comment will be appreciate. ^_^
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: awt event handling