Dez 102009

Sometimes a customer asks for a mechanism to lock a Java desktop application after a certain time of inactivity by the user.

This can be easily achieved by adding a listener to the toolkit that contains a timer counting backwards. Every time the user creates a [cci lang=“java“]MouseEvent[/cci] or a [cci lang=“java“]KeyEvent[/cci] the timer is set back. If the timer fires because there has been no activity for a specified period of time a modal dialog can be opened to force the current user to re-enter his password.

Adding the listener is quite easy and should be done once the main frame is created:

// create the listener and set the timeout to 20 minutes
IdleListener idleListener = new IdleListener(20);
// add the listener to the toolkit's listener list and let it react on 
// key and mouse events
                            AWTEvent.KEY_EVENT_MASK | 
// start the listener's timer
// no show the frame and so on...

So now let’s move on to the implementation of the [cci lang=“java“]IdleListener[/cci]:

import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

 * This listener reacts to timeouts and forces the user to enter his
 * password before allowing him to continue working.
 * The timer is being reset at every AWT event.
public class IdleListener implements AWTEventListener, ActionListener {

    private Timer timer;
    private int timeoutMinutes = 20;

     * Creates a new timer and registers itself as action listener.
    public IdleListener(int timeoutMinutes) {
        this.timeoutMinutes = timeoutMinutes;
        timer = new Timer(timeoutMinutes * 60000, this);

     * Invoked when an event is dispatched in the AWT. Simply
     * resets the timer.
    public void eventDispatched(AWTEvent event) {

     * Invoked when an action occurs (i.e. the timer triggers).
    public void actionPerformed(ActionEvent e) {
        // create a modal dialog and let the user enter his password.
        // ... code for dialog goes here ....
        // after successful authorization, restart the timer and move on.

     * stopps the timer.
    public void stopTimer() {

     * starts the timer.
    public void startTimer() {
        timer = new Timer(timeoutMinutes * 60000, this);


  4 Responses to “Locking a Java client application”

Comments (4)
  1. I noticed your website’s ranking in google’s search results is
    very low. You are loosing a lot of traffic. You need
    hi Page Rank backlinks to rank in top ten. I know – buying them is too expensive.
    It’s better to own them. I know how to do that, just google it:
    Polswor’s Backlinks Source

  2. Ask as many questions as you want, and ask to speak with prior patients to get more
    information regarding the whole experience. However,
    as long as recovery instructions are followed most women do not experience
    anything other than the expected discomfort from surgery.

    According to the Plastic surgery Bonita Springs, the primary difference between the
    two surgeons is the training that they undergo.

  3. I read a lot of interesting posts here. Probably you spend a lot of time writing,
    i know how to save you a lot of work, there is an online tool that creates unique, google friendly articles
    in minutes, just type in google – laranitas free content source

  4. It’s hard to find your articles in google.
    I found it on 21 spot, you should build quality backlinks , it will help you to rank to google top 10.
    I know how to help you, just type in google – k2 seo tips

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>