Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java:Show message dialog for 10sec and remove?

Tags:

java

swing

Hi i want to show JOptionPane.showMessageDialog(null, "Appication already running");

for 10sec and then remove it.how can i dothat?

like image 841
Harinder Avatar asked Jun 30 '11 05:06

Harinder


2 Answers

You can create a JOptionPane manually, without static methods:

JOptionPane pane = new JOptionPane("Your message", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(parent, "Title");

then you can show the dialog and fire up a timer to hide it after ten seconds.

like image 129
Denis Tulskiy Avatar answered Nov 01 '22 05:11

Denis Tulskiy


I tried these answers and ran into the problem that showing the dialog is a blocking call, so a timer can't work. The following gets around this problem.

      JOptionPane opt = new JOptionPane("Application already running", JOptionPane.WARNING_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}); // no buttons
  final JDialog dlg = opt.createDialog("Error");
  new Thread(new Runnable()
        {
          public void run()
          {
            try
            {
              Thread.sleep(10000);
              dlg.dispose();

            }
            catch ( Throwable th )
            {
              tracea("setValidComboIndex(): error :\n"
                  + cThrowable.getStackTrace(th));
            }
          }
        }).start();
  dlg.setVisible(true);
like image 8
AndriesH Avatar answered Nov 01 '22 04:11

AndriesH