This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Swing Timer

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm really new to programming and Java but I'm really getting into to it. I'm writing a small program that is going to use the Swing Timer and I've got it to work but I'm not sure about two things.

Question #1: This works but if I take the try statement that puts the thread to sleep it will not work. I don't understand why you'd need the thread to sleep. Shouldn't the scheduler run it right away?

Question # 2: I didn't see anything in the API or the tutorials about adding in a the Try-Sleep code. How would I know to do this (understanding Threads better I guess?)

Question # 3: in the import, this doesn't work but i thought adding " import javax.swing.*;" would also bring in the Timer class...?

Thank you for your help.

Code-

import javax.swing.Timer;
import javax.swing.*;
import java.awt.event.*;

public class Test {

public static void main(String[] args) {
Test test = new Test();
test.go();
}

public void go () {

System.out.println("in go");

Timer timer = new Timer(6000,new listen());
timer.start();
timer.setRepeats(false);

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}

class listen implements ActionListener {
public void actionPerformed(ActionEvent e) {

System.out.println("In actionevent");
JFrame frame = new JFrame();
frame.setSize(100,100);
frame.setVisible(true);
}
}
}
 
Marshal
Posts: 65062
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please use code tags round quoted code; they make it easier to read.
Long time since I used Timers, but as far as I can remember:

  • There are two other classes called Timer; you can get confusion between them.
  • You have obviously got your delay of 6 seconds before "in actionevent" prints. That works correctly.
  • You have your 1 second delay before printing out "in go" correct.
  • You have obviously worked out how to use this Timer.
  • What you appear to have is three threads.
  • Main thread. This starts off your app, and starts whenever a main method is called.
  • Event dispatch thread. This is (or ought to be) started whenever you start off any Swing objects.
  • Another thread started by the Timer, let's call it timerThread.
  • They run rather like this

    main---------> etc etc
    Starts off event dispatch thread (EDT).
    EDT--->print "in go"-->sleep 10 sec----------------------->------------------------------->nothing happens
    Starts off timerThread (TT)
    TT-->starts--->waits 6 seconds-->print "event" & make frame visible--->don't repeat.

    There is a problem with Thread.sleep(); it may throw an InterruptedException if another Thread calls to interrupt it while "asleep." InterruptedException is a checked Exception so the try-catch is required.

    Suggest:
  • Add a setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) call on your frame, so you can close the app with the close button.
  • Put something inside the catch block, so you can see whether the Thread is interrupted.
  • Consider whether you need the sleep call at all; it doesn't appear to do anything.
  • OR: Add something after the sleep() call so you can see when it finishes.
  • Read this about Swing and Threads, and this part of the Java Tutorial about Threads.[edit]Slight formatting corrections[/edit]
    [ January 10, 2008: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 65062
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually I was mistaken about your print "in go". I thought there was a 1-second delay before it, and there is a 10-second delay afterwards.
     
    jorge Garcia
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the response and the suggested tutorials. I'll check them out.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!