Solved

Make buttons work in a runnable applet

Posted on 2004-04-18
5
825 Views
Last Modified: 2013-11-23
OK, it's been a long time since I did anything with java and I run into a stumper.  I have an applet (public class animate extends Applet implements Runnable  ) and in general terms it operates a counter within it that determines the behaviour of the applet.  Anyways, my problem is that I would like to load the applet and make it start based on a button but I can't figure out how.  In my init() I have a button (one button only)
      
                StartB.addActionListener(this);
      add(StartB);

Now the compiler complains as follows:  "Value for argument 1 cannot be converted from 'animate' in call to 'voidButton.addActionListener(ActionListener)"  This error refers to StartB.addActionListener(this);  I think the problem is that it implements Runnable instead of a ActionListener, but I think I had it working once (when I did more java) under runnable without implementing action listener.

OK, what am I missing and what is the simplest fastest solution to this problem?  Btw, I'm running Visual J++ on a win2k Virtual Machine.

Thanks
0
Comment
Question by:Kejtar
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10856243
your applet class needs to implement ActionListener

public class animate extends Applet implements Runnable. ActionListener

You also need to add an actionPerformed() method which will get called when the button is pressed.

public void actionPerformed(ActionEvent event)
{
   // do whatever required here
}
0
 
LVL 1

Author Comment

by:Kejtar
ID: 10856314
ok, but I need to implement Runnable as well, and IIRC java right it's one or the other: can't implement both Runnable and ActionListener at the same time, or can I?

Btw, I do have actionPerformed, but....  due to the button problems, I can't get to it :(
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 10856321
> can't implement both Runnable and ActionListener at the same time, or can I?

you can.

A little type in the code i posted above it should have been a comman and not a dot.

public class animate extends Applet implements Runnable, ActionListener
0
 
LVL 1

Author Comment

by:Kejtar
ID: 10856344
Thanks, that was it.  This also proves my theory that all problems in programming revolve around comma, period or semi colon :D
0
 
LVL 92

Expert Comment

by:objects
ID: 10856358
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now