Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Timed Option Pane

63 views
Skip to first unread message

Paul Thompson

unread,
Feb 12, 2009, 10:13:51 AM2/12/09
to
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?

Thanks
Paul

John B. Matthews

unread,
Feb 12, 2009, 10:58:20 AM2/12/09
to
In article <GOKdnXZUM804oQnU...@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).

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Fred

unread,
Feb 12, 2009, 11:11:52 AM2/12/09
to
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.

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.

John B. Matthews

unread,
Feb 12, 2009, 3:46:35 PM2/12/09
to
In article
<eba3add8-07d9-4180...@z27g2000prd.googlegroups.com>,
Fred <fred.l.kl...@boeing.com> wrote:

> 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>

rp0000024133ua

unread,
Feb 12, 2009, 5:35:43 PM2/12/09
to

Thanks for the help.

0 new messages