• Post Reply Bookmark Topic Watch Topic
  • New Topic

Time out a Swing application by inactivity  RSS feed

 
Alejandro Barrero
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be possible to timeout a Swing application by user inactivity, but I have not been able to figure it out or find it in the Internet. I can set a timer as a mouse and key listener in the main frame, but event in internal frames or dialogs do not trigger the timer actions. Could someone please tell me how to do this?

Thanks
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the javadocs for Toolkit.addAWTEventListener()
 
Alejandro Barrero
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I created the class TimeoutListener implementing AWTEventListener and in the main frame I use:
Toolkit.getDefaultToolkit().addAWTEventListener(timeoutListener, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK.
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this class..


public final class SysEventQueue extends EventQueue
{
private static Date last_act_time;
private static long timeout_long;
private static SysEventQueue Queue;

/*
* Start the Inactivity timer
*/
public static void startInactivityTimer()
{
try
{
Queue = new SysEventQueue();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(Queue);
}
catch (Exception e)
{

}
finally
{

}
}

/*----------------------------------------------------------------------------*/
/**
* Constructor.
*/
/*----------------------------------------------------------------------------*/
private SysEventQueue()
{
last_act_time = new Date();
}

/*----------------------------------------------------------------------------*/
/**
* Dispatch Event. Monitors User Activity.
*/
/*----------------------------------------------------------------------------*/
protected void dispatchEvent(AWTEvent e)
{
super.dispatchEvent(e);
if (e.getID() == MouseEvent.MOUSE_RELEASED || e.getID() == KeyEvent.KEY_RELEASED)
{
last_act_time = new Date();

}
}

/*----------------------------------------------------------------------------*/
/**
* Pop the pushed EventQueue.
*/
/*----------------------------------------------------------------------------*/
public void pop()
{
try
{
super.pop();
}
catch (EmptyStackException e)
{
}
}

/*----------------------------------------------------------------------------*/
/**
* Timeout or not
*/
/*----------------------------------------------------------------------------*/

public static boolean notTimeout()
{
boolean nottimeout = true;

try
{
// Minute to mili sec
timeout_long = getTimeOutValue(); //to be defined to return the time out value.
}
catch (Exception on)
{
timeout_long = 0;
}

// Check if inactivity time is more than that specified.
Date cur_time = new Date();

if ((cur_time.getTime() - last_act_time.getTime()) > timeout_long)
{
System.out.println("DISCONNECTING ..............\n");
System.out.println("********" + timeout_long + "*********\n");
System.out.println("Curtimr:" + cur_time);
System.out.println("acttime:" + last_act_time);
}
Queue.pop();
nottimeout = false;
}

return nottimeout;
}

private long getTimeOutValue()
{
return 60000; //for 1 min
}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!