Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Timer and TimerTask class 1

Status
Not open for further replies.

aryajur

Programmer
Jul 2, 2003
45
US
I am using the Timer and the TimerTask classes in one of my MIDlets to generate an action after 3 seconds.
Actually what I want to do is generate Action 1 if a button is pressed and released for less than 3 seconds and I want to generate Action 2 if a button is pressed and released for >= 3 seconds.
I amusing the methods keyPressed() and keyReleased () of the canvas class. When a key is pressed I make a new Timer instance and schedule a TimerTask to generate Action 2. But if the key is released i.e keyReleased is called I check if the scheduled task has already been done or not if not I cancel the Timer and the TimerTask and generate Action 1. The problem is cancelling the instances has no effect and the Action 2 is still generated if I momentarily press and release the button. The code listing of keyPressed and keyReleased is as follows:

protected void keyPressed(int keyCode)
{
switch (keyCode)
{
case KEY_NUM1:
Sec3ThreadT1 = new Timer();
threeSecT1 = new ThreeSecPressAction(true, this);
Sec3ThreadT1.schedule(threeSecT1, 3000);
break;
case KEY_NUM3:
Sec3ThreadT2 = new Timer();
threeSecT2 = new ThreeSecPressAction(false, this);
Sec3ThreadT2.schedule(threeSecT2, 3000);
break;
}
}

protected void keyReleased(int keyCode)
{
switch (keyCode)
{
case KEY_NUM1:
Sec3ThreadT1.cancel();
threeSecT1.cancel();
if(threeSecDoneT1)
{
//Action 2 Already done by Timer
threeSecDoneT1 = false;
}
else
{
//Generate Action 1
}
break;
case KEY_NUM3:
Sec3ThreadT2.cancel();
threeSecT2.cancel();
if(threeSecDoneT2)
{
//Action 2 Already done by Timer
threeSecDoneT2 = false;
}
else
{
//Generate Action 1
}
break;
}
}



Why is it when I press the button briefly I get Action 1 and Action 2 both. But when I keep the button pressed for 3 seconds I get only Action 2??

Please help me !!!
 
Well first you only need one Timer object that gets created at application initialization time not on each keypress event. Here is some code from a JFrame application that does what you describe, maybe it will help.
Code:
public class Main extends javax.swing.JFrame {
	class F1TimedHandler extends java.util.TimerTask{
		protected Main _main;
		public F1TimedHandler(Main main){_main = main;}

		public void run() {
			System.out.println("F1TimedHandler");
			_main._bF1Handled = true;
		}
	}
	public boolean _bF1Handled = false;
	protected java.util.Timer _timer;
	protected F1TimedHandler _handler = null;
	/** Creates new form Main */
    public Main() {
        initComponents();
		  _timer = new java.util.Timer();
    }
	 private void formKeyReleased(java.awt.event.KeyEvent evt) {
		 // Add your handling code here:
		 switch( evt.getKeyCode()){
			case java.awt.event.KeyEvent.VK_F1:
				System.out.println("F1 Released");
				_handler.cancel();
				if( !_bF1Handled)
					System.out.println("Before time went off");
				_handler = null;
				break;
		 }
	 }

	 private void formKeyPressed(java.awt.event.KeyEvent evt) {
		 // Add your handling code here:
		 switch( evt.getKeyCode()){
			case java.awt.event.KeyEvent.VK_F1:
				if( null == _handler){
					System.out.println("F1 Pressed");
					_bF1Handled = false;
					_handler = new F1TimedHandler(this);
					_timer.schedule(_handler, 2000);
				}
				break;
		 }
	 }
.... rest of class

-pete
 
Hi,
Thanks pete. The code u gave me works fine. Thanks for the help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top