• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Time out a Swing application by inactivity

 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the javadocs for Toolkit.addAWTEventListener()
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}

}
 
reply
    Bookmark Topic Watch Topic
  • New Topic