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

Add event listener.... Help!

Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
Hi,
I'm trying to add and event Listener to a button in an applet.
I finally got my event listener class to compile and now the only trouble is that the addEventListener("name of listener object") line in my applet gives me this error:
addActionListener(java.awt.event.ActionListener) in java.awt.Button cannot be applied to (MyListener) // the name of my Listener class

Here's the code for the listener class so you can spot any huge gaping errors there may be :-)

CODE FOR MYLISTENER CLASS:
import java.awt.*;
import java.io.*;
// Event Listener class for buttons
class MyListener extends SMSApplet
{
public void actionPerformed( TextArea t1, TextArea t2 )
{
try
{
t2.setText( breakText( t1.getText() ) );
}
catch ( IOException io )
{}

}
}

Thanks in advance for any help you can give me.

Terry


Raising Flares debut album 'Ignition' out now

http://www.raisingflares.com

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Terence,
Events and Listeners are one of the more confusing topics so don't feel bad if this seems like a "Doh!" moment
ActionListener is an interface, so your listener class will have to implement it. Furthermore, the only method in the ActionListener interface is actionPerformed which takes a single parameter of the type ActionEvent.
That means that semantically, you have two glaring errors:
<pre>
class MyListener extends SMSApplet
</pre>
should be
<pre>
class MyListener extends SMSApplet implements ActionListener
</pre>
and the line
<pre>
public void actionPerformed(TextArea t1, TextArea t2)
</pre>
should be
<pre>
public void actionPerformed(ActionEvent ae)
</pre>
There is another problem with your code though. You appear to want to access two TextArea objects. I assume these are on your main applet. So, if I read between the lines right, you have another applet that has at least two TextAreas and one button. What you want to happen is when you click the button, you will call a method breakText() passing it one TextArea object and put the resulting String in the other TextArea object.
If this is the case, you don't need to make a separate Listener class. All you need to do is make your main applet implement the ActionListener interface, then add the actionPerformed method below:
<pre>
public void actionPerformed(ActionEvent ae)
{
if ( ae.getSource() == theButton )
{
try
{
t2.setText( breakText( t1.getText() ) );
}
catch ( IOException ioe )
{
...
}
}
}
</pre>
Then your code for addActionListener would be something like this:
<pre>
theButton.addActionListener( this );
</pre>
That makes your applet a Listener for ActionEvents generated by theButton.
There are other approaches such as using an anonymous inner class but the above should work. The Java Tutorial has a good trail on writing applets and it touches on using Actions too.

Junilu - [How to Ask Questions] [How to Answer Questions]
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
Thank you very much for your help Junilu. I actually got the very same idea off a web tutorial on applet buttons. I had at some stage put in the interface but I had taken it out again as I was getting so many errors. The code I had at the end just had the one error about the interface although the error message didn't explicitly say it...
Many thanks,
Terry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add event listener.... Help!