JOptionPane with Timeout

Hello All,

I am putting a new version of my application out tommorow where I have replaced all of the JOptionPane's with one's that timeout after a given time limit. I have tested it and it seems to work fine on my environment, does anyone see any problems with the code? The last thing I want is to find potential problems after I release it.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.beans.*;

/**

*

* @author Malcolm F

*/

publicclass TimeoutOptionPane{

publicstatic Object showTimeoutPane(final JOptionPane optionPane,

final Frame frame,final String title,finalint timeoutMillis ){

// Listen for property changes in the JOptionPane

optionPane.addPropertyChangeListener(new PropertyChangeListener(){

publicvoid propertyChange( PropertyChangeEvent e ){

if ( e.getPropertyName().equals( JOptionPane.VALUE_PROPERTY ) ){

synchronized ( e.getSource() ){

e.getSource().notify();

}

}

}

} );

// Create dialog based on JOptionPane and set visible

final JDialog timeoutDialog = optionPane.createDialog( frame, title );

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

timeoutDialog.setVisible(true );

}

} );

// Wait for timeout, notified early if value property changes

synchronized ( optionPane ){

try{

optionPane.wait( timeoutMillis );

}catch ( Exception e ){

e.printStackTrace();

}

}

// Dispose of dialog

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

timeoutDialog.dispose();

}

} );

return optionPane.getValue();

}

}

[3612 byte] By [Malcolm_Fa] at [2007-11-26 21:46:51]
# 1

There are definitely some suspicious design choices here...

For example... stuff like this should never make it into a production system:

catch ( Exception e ) ...

Here is question: Why not use a Timer and the simple publish-subscribe pattern? What i'm thinking is that you have a timer and JOptionPane's add an expiry action to it. In that case there is no "synchronize", "notify", nor "wait" constructs required.

jvaudrya at 2007-7-10 3:36:43 > top of Java-index,Desktop,Core GUI APIs...