Results 1 to 3 of 3
  1. #1
    bumblyb33 is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default Using ActionListener with a Timer Object


    Can someone please have a look at this code and telling me what I'm doing wrong. I'm trying to get a message printed every two seconds but whenever I run the program, nothing is printed.

    Java Code:
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import javax.swing.Timer;
    public class Testing {
    	private int id;
    	public Testing(int id)
    	{ = id;
    	public void setTime(GregorianCalendar appt)
    	//	GregorianCalendar now = new GregorianCalendar();
    	//	this.appt = appt; //(int)appt.getTimeInMillis()-(int)now.getTimeInMillis()+10
    		Timer t = new Timer (2000, new ActionListener ()
             public void actionPerformed (ActionEvent e)
            	 System.out.println("You have an appointment in 10 minutess.");
    	public boolean equals(int otherId)
    		if( == otherId)
    			return true;
    			return false;

    Java Code:
    import java.util.GregorianCalendar;
    public class Tester {
    	public static void main(String[] args) throws InterruptedException {
    		// TODO Auto-generated method stub
    		Testing appt = new Testing(1);
    		GregorianCalendar date = new GregorianCalendar(2009, 3, 4, 13,45);

    I'm supposed to use the date as an argument, but I'm using 2000ms just for now to test it, so that's why the GregorianCalendar is commented.


  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    javax.swing.Tmer is designed to run the listening method on the Event Dispatcher Thread, which is where all work for Swing components takes place. Since you don't have any active Swing components, I don't know what the Timer would do, quite possibly nothing. Is that what you are seeing?

    Create a simply Swing UI and have the Timer ActionPerformed update the UI.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Either that, or if this is never to be used in a Swing program, then look into using java.util.Timer instead.

Similar Threads

  1. Replies: 0
    Last Post: 12-31-2008, 02:20 PM
  2. Demonstrating the ActionListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:20 PM
  3. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts