I am trying to run a small example which i found from here http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html . However, I get Type mismatch: cannot convert from void to Timer on the timer = new Timer….. line. I was hoping someone could help me.
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
Thank you
my code;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class MyTimerTest
{
private JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
Timer timer;
int delay = 1000; //a second
public MyTimerTest()
{
}
public void runTimer()
{
ActionListener taskPerformer = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
}
};
timer = new Timer(delay, taskPerformer).start();
}
/**
* @param args
*/
public static void main(String[] args)
{
}
}
Your code does not contain line
timer = ....I believe you tried to write:
Timer timer = new Timer(delay, taskPerformer).start();This cannot be compiled because method
start()is void.Just separate this line into 2:
EDIT, oh, my assumption is correct. I just have not seen the second line where you are using timer in the second fragment of your code.