-1

I am developing a application. I want when I exit it, a message box appears having message, for example, "Thanks for using Soft Attendance". Then disappear automatically say after few seconds.

I have write code for this as following:

public void windowClosing(WindowEvent e){
        int whichOption;
        whichOption=JOptionPane.showConfirmDialog(f1,"Are you Serious?","Soft Attendence",JOptionPane.YES_NO_OPTION);
        if(whichOption==JOptionPane.YES_OPTION){
            f1.dispose();
            JOptionPane.showMessageDialog(null,"Thanks for using Soft Attendence");
        }
    }

When i click on exit button a confirmation dialog box appear and after clicking yes button application is exited and a another message box appears.

Now I have two questions in my mind :

First question is I have dispose application already and then message box is shown. Is it fine to show message box after its parent is killed?

When second message box appears I don't want to click on "OK" button. I want it should automatically disappear. So my second question is How to shown message box that disappear automatically after some time?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32

1 Answers1

1
Is it fine to show message box after its parent is killed? 

I think this would not be a ideal case. But you can open dialog if the parentComponent has no Frame JOptionPane.showConfirmDialog(null,"...");

How to shown message box that disappear automatically after some time?

Auto disposable you can get it by a trick, you can create a SwingWorker which normally performs GUI-interaction tasks in a background thread. Set a timer and which will execute after time period and close your dialog explicitly.

class AutoDisposer extends SwingWorker<Boolean, Object> {
   @Override
   public String doInBackground() {
       try{
           JOptionPane.getRootFrame().dispose();
           return Boolean.True;
       }catch(Exception ex){...}
       return Boolean.False;
   }
}

...

new Timer(seconds * 1000, autoDisposer).start();
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • 2
    JOptionPane.getRootFrame().dispose(); must be wrapped into invokeLater, doInBackground() executing Workers Thread, never notified EDT – mKorbel Mar 03 '14 at 11:21
  • Thanks....but i have done it in other way,that by first hiding app ,then showing dialog box after waiting sometime with Thread.sleep(), then disposing the app.... – A.s. Bhullar Mar 06 '14 at 09:00