Thanks
Paul
> I'd like to put up a pane that expects an "OK" response, but times out
> after a few seconds if no response is given. How would I go about this?
Create a javax.swing.Timer; in the listener, let a java.awt.Robot press
the desired key (untested).
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
This is the wrong (or at least unnecessary) use of a Robot.
Just create a function that dismisses the pane and performs whatever
functionality is desired, and have both the pane's listener and the
Timer's listener call that function.
--
Fred K.
> On Feb 12, 7:58 am, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > In article <GOKdnXZUM804oQnUnZ2dnUVZ_hydn...@posted.hiwaay1>,
> > Paul Thompson <pa...@hiwaay.net> wrote:
> >
> > > I'd like to put up a pane that expects an "OK" response, but
> > > times out after a few seconds if no response is given. How would
> > > I go about this?
> >
> > Create a javax.swing.Timer; in the listener, let a java.awt.Robot
> > press the desired key (untested).
> >
>
> This is the wrong (or at least unnecessary) use of a Robot.
Quite right.
> Just create a function that dismisses the pane and performs whatever
> functionality is desired, and have both the pane's listener and the
> Timer's listener call that function.
This is the preferred way. Oddly, updating the message moves focus from
the OK button. Any insight?
<sscce>
package gui;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/** @author John B. Matthews */
public class JOptionTest extends JDialog
implements ActionListener, PropertyChangeListener {
private static final int TIME_OUT = 10;
private int count = TIME_OUT;
private final Timer timer = new Timer(1000, this);
private final JOptionPane optPane = new JOptionPane();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new JOptionTest().createGUI();
}
});
}
private void createGUI() {
this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
timer.setCoalesce(false);
optPane.setMessage(message());
optPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
optPane.setOptionType(JOptionPane.DEFAULT_OPTION);
optPane.addPropertyChangeListener(this);
this.add(optPane);
this.pack();
this.setVisible(true);
timer.start();
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (JOptionPane.VALUE_PROPERTY.equals(prop)) {
thatsAllFolks();
}
}
public void actionPerformed(ActionEvent e) {
count--;
optPane.setMessage(message());
if (count == 0) thatsAllFolks();
timer.restart();
}
private String message() {
return "Closing in " + count + " seconds.";
}
private void thatsAllFolks() {
this.setVisible(false);
System.exit(0);
}
}
</sscce>
Thanks for the help.