Stop a swing Timer from inside the Action Listener

Also possible:

final Timer timer = new Timer(10, null);
timer.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        (as in the question, except that you can refer to timer here)
    }
});

Or, use the event object to get the source (and cast it, boo):

final Timer timer = new Timer(10, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        ((Timer)evt.getSource()).stop();
    }
});

Or, keep the timer in an instance variable and you can reference it from your handler or have the handler call a method on your class which could stop/start it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)