Results 1 to 3 of 3
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Timer and TimerTask

    I'm trying to use a Timer and a TimerTask that starts when a key is pressed, then stops when it is released. I'm not sure why I'm getting this error:

    Exception in thread "AWT-EventQueue-1" java.lang.IllegalStateException: Task already scheduled or cancelled

    I would think that it is started, then cancelled, started, cancelled, etc. Given this, how can I get this exception? Here are the relevant code fragments:

    Java Code:
    	public Timer timer = new Timer();
    	public TimerTask
    		LUp = new TimerTask()
    		{
    			public void run()
    			{
    				if(LY > 35)
    				{
    				LY-=10;
    				}
    			}
    		},
    		LDown = new TimerTask()
    		{
    			public void run()
    			{
    				if(LY < 365)
    				{
    				LY+=10;
    				}
    			}
    		},
    		RUp = new TimerTask()
    		{
    			public void run()
    			{
    				if(RY > 35)
    				{
    				RY-=10;
    				}
    			}
    		},
    		RDown = new TimerTask()
    		{
    			public void run()
    			{
    				if(RY < 365)
    				{
    				RY+=10;
    				}
    			}
    		};
    ...

    Java Code:
    	public void keyPressed(KeyEvent e)
    	{
    		switch (e.getKeyCode())
    		{
    		case KeyEvent.VK_W:
    			timer.scheduleAtFixedRate(LUp, 0, 30);
    			break;
    		case KeyEvent.VK_S:
    			timer.scheduleAtFixedRate(LDown, 0, 30);
    			break;
    		}
    
    		switch (e.getKeyCode())
    		{
    		case KeyEvent.VK_UP:
    			timer.scheduleAtFixedRate(RUp, 0, 30);
    			break;
    		case KeyEvent.VK_DOWN:
    			timer.scheduleAtFixedRate(RDown, 0, 30);
    			break;	
    		}
    	}
    
    	public void keyReleased(KeyEvent e)
    	{
    		switch (e.getKeyCode())
    		{
    		case KeyEvent.VK_W:
    			LUp.cancel();
    			break;
    		case KeyEvent.VK_S:
    			LDown.cancel();
    			break;
    		}
    
    		switch (e.getKeyCode())
    		{
    		case KeyEvent.VK_UP:
    			RUp.cancel();
    			break;
    		case KeyEvent.VK_DOWN:
    			RDown.cancel();
    			break;	
    		}
    	}
    Last edited by AndrewM16921; 04-07-2009 at 11:31 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Is this a Swing application?

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

Similar Threads

  1. Timer help
    By Kinyo in forum New To Java
    Replies: 15
    Last Post: 03-15-2009, 02:37 AM
  2. EJB Timer
    By mrjunsy in forum Advanced Java
    Replies: 0
    Last Post: 08-22-2008, 04:09 PM
  3. EJB Timer
    By mrjunsy in forum New To Java
    Replies: 0
    Last Post: 08-04-2008, 06:47 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. TimerTask with a Date_Time
    By kunta in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 05-31-2007, 10:17 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
  •