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();
}
}

