Is it possible to make a timer based dialog/JOptionPane which will close after a preset time?

Sandip Chitale

You could override the setVisible(boolean show) method of the Dialog class. When the setVisible(true) is called you could start a thread and wait for preset amount of time in its run method. If the wait is over you could call setVisible(false) on the Dialog and exit the thread. If the dialog hidden (setVisible(false)) before the preset time expires you just stop the thread.

Here is the pseudo code -

private Thread hider;

public void setVisible(boolean show) {
  if (show) {
    // show case
    if (hider != null) {
       hider = new Thread() {
            public void run() {
              try {
              } catch (InterruptedException e) {
              // PRESET_TIME expired
       // start the timeout thread
  } else {
    // hide case
    if (hider != null) {
      if (hider.isAlive()) {
      hider = null;

  // do the default thing

You will not be able to use the static methods in the JOptionPane class to show the various kinds of JOptionPanes. That is because you do not have control over the wrapper Dialog creation in that case. However you could use the JOptionPane contructors.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact