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

    Default Using ActionListener with a Timer Object

    Hellooo

    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)
    	{
    		this.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.");
              }
            });
    		
    		t.start();
    		
    	}
    	
    	public boolean equals(int otherId)
    	{
    		if(this.id == otherId)
    			return true;
    		else
    			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);
    		appt.setTime(date);
    		
    	}
    
    }


    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.

    Thx

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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, 03:20 PM
  2. Demonstrating the ActionListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 09:20 PM
  3. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 09:19 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 11: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
  •