Solved

Make buttons work in a runnable applet

Posted on 2004-04-18
5
831 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 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

26 Experts available now in Live!

Get 1:1 Help Now