Java Tips Weblog

  • Blog Stats

    • 2,076,564 hits
  • Categories

  • Archives

  • Advertisements

Application Inactivity

Posted by Rob Camick on October 24, 2008

There are times when you may want to monitor your application for inactivity. Maybe you want to log out a user after a certain time interval. This requirement can be broken down into two main steps. We will need to:

  • listen for events at the application level
  • track the time interval between these events

Whenever the time interval between events exceeds our inactivity threshhold we will then invoke the inactivity Action.

Luckily for us there is an easy solution to both steps. First, we can use an AWTEventListener to listen for events. This listener can be added to the Toolkit class to listen for events generated by the AWT system. We also have the ability to control the events we want to listen for. Secondly, we can use the Swing Timer to control the inactivity interval. The timer is setup to invoke an inactivity Action after a specified interval. However, when the AWTEventListener receives an event it can simply tell the timer to reset itself. So as long as events are received the timer will never fire.

The functionality of these two classes has been combined into the InactivityListener class. The following information is required to use the class:

  • the Action to invoke (when the Timer fires)
  • the inactivity interval
  • an event mask used by the AWTEventListener

You can create many different event masks to be used by the AWTEVentListener. However, I have provided a few common masks that you are likely to use:

  • MOUSE_EVENTS (includes MouseMotionEvents)
  • USER_EVENTS (combines KEY_EVENTS and MOUSE_EVENTS), this is the default

So, to schedule an automatic logout from your application after 10 minutes of no keyboard or mouse activity you would do something like:

JFrame frame = new JFrame(...);
frame.setVisible( true );

Action logout = new AbstractAction()
	public void actionPerformed(ActionEvent e)
		JFrame frame = (JFrame)e.getSource();

InactivityListener listener = new InactivityListener(frame, logout, 10);

Get The Code

Related Reading

How to Use Swing Timers
Java API: java.awt.ToolKit.addAWTEventListener(…)
Java API: java.awt.AWTEvent


14 Responses to “Application Inactivity”

  1. Anonymous said

    The article and the given program is very useful. I am very thankful to you for writing the same. Thanks a lot. Please keep writing such useful articles and such programs.

  2. Anonymous said

    can you show me a sample of implementation of Action logout = new YourLogoutAction(…);? thanks

  3. Anonymous said


    This works only for your application but not capture whole windows inactivity. I mean mouse over or key press on your application is captured but not the one done on windows or any other application. How do I know if user is doing any activity on machine and not necessarily on my application.

  4. Anonymous said

    This is a very useful program indeed. This is exactly what I need. I’m going to use your code. :) Thanks so much!!!…

  5. Anonymous said

    Useful Info….!!!

  6. Anonymous said

    Useful! I will be using it.

  7. Anonymous said

    Thank you a lot for very helpful topic. this is idea that I am searching.


  8. Anonymous said

    Many thanks for this topic. After much searching I come across this and in 1 page you provided an answer of what I needed. Thanks

  9. Anonymous said

    Thank you. Even after 8 years, you still help someone with your posts.

  10. Ashutosh Tiwari said

    Hi Rob Camick, this is very useful articles, Can I also catch the timer progress so that I can also add message in GUI, dialog is about to close

  11. Randika Perera said

    Thank you very much! Your article saved me form lot of hassles.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: