Barak
Barak

Reputation: 107

TimerTask runs just once

I have a task named timer:

timer.schedule(new task1(), 1000*minutes);

The task:

class task1 extends TimerTask {
        @Override

    public void run()
    {
            try {
                task();
            } catch (SAXException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (URISyntaxException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    public void task() throws SAXException, ParserConfigurationException, IOException, URISyntaxException, InterruptedException {
        Pinner_xml t = new Pinner_xml();
        t.xml(frame.t1.getText());

        frame.output.append("task 1 \n");
        System.out.println("task 1 is running");
    }
}

frame is my frame name and output is a text box. Why I run the task all is "task 1 is running" in the output dialog box in the Netbeans and the task runs only once.

Upvotes: 1

Views: 2933

Answers (3)

nIcE cOw
nIcE cOw

Reputation: 24616

Use javax.swing.Timer for Swing, as everything is done on the EDT (by default) using Timer Class, which is the prerequisite, See how to use Timer. One more related example for updating JButton on a Timer and another for working with Swing Timer and Scrolling Text

Upvotes: 9

Yahya Arshad
Yahya Arshad

Reputation: 1626

Read documentation about Timer There is another overridden method to repeatedly execute a task, you are using method which executes only once You can use

scheduleAtFixedRate(TimerTask task, long delay, long period)

or

schedule(TimerTask task, long delay, long period)

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

For repeated task execution use Timer.schedule(TimerTask task, long delay, long period) or scheduleAtFixedRate methods

Upvotes: 5

Related Questions