Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

awt event handling

 
ravi ckumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant Any comment will be appreciate. ^_^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic