0

I would like to display the button "OK" of this JOptionPane only after a certain amount of time (let's say for example 5 sec). (My aim is actually to let finish some thread work behind this other thread)

JOptionPane jop2 = new JOptionPane();   
jop2.showMessageDialog(null, "Please wait 5s", "WAIT", JOptionPane.INFORMATION_MESSAGE);

I don't know at all how to do that, could you provide me some code working which will answer to this problem? Thank you very much in advance!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The user can still dismiss the `JOptionPane` within the 5 seconds (even before the button appears). It seems also that '5 seconds' is quite arbitrary. The option to close should be triggered by thread completion. – Andrew Thompson Aug 30 '12 at 00:43
  • 1
    _provide me some code_ that's not how this site is working: its idea is to help _you_ to provide the code yourself :-) – kleopatra Aug 30 '12 at 06:45

3 Answers3

4

There is no specific way to do this using JOptionPane. You will have to create a custom dialog and reveal the OK button after a fixed time. You could use one pass of a Swing timer.

ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        button.setVisible(true);
    }
};

Timer timer = new Timer(0, taskPerformer);
timer.setInitialDelay(5000);
timer.setRepeats(false);
timer.start();
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

It sounds like what you're looking for is a combination of the SwingWorker and the ProgressMonitor. The SwingWorker will preform your long running task (the 5 second one), and inform the user of how it's progressing using the ProgressMonitor. An example showing you how to get the two working together can be found here: getting the cancel event of Java ProgressMonitor

Of course, if you're convinced you want to take the approach of displaying the continue button once the work is done, here's an example that should get you started in the right direction. You'll use a SwingWorker to alert your Dialog that the long running background task has completed.

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import javax.swing.*;

public class TempProject extends Box{

    public TempProject(){
        super(BoxLayout.Y_AXIS);

        //Contains the content of the Alert Dialog
        Box info = Box.createVerticalBox();
        info.add(new Label("Please wait 5 seconds"));
        final JButton continueButton = new JButton("Continue");
        info.add(continueButton);

        //The alert to wait 5 seconds
        final JDialog d = new JDialog();
        d.setTitle("WAIT");
        d.setModalityType(ModalityType.APPLICATION_MODAL);
        d.setContentPane(info);
        d.pack();

        //The action of the "Continue Button"
        continueButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                d.dispose();
            }
        });
        continueButton.setVisible(false);

        //Thread That Does Work
        final SwingWorker sw = new SwingWorker<Integer, Integer>()
        {
            protected Integer doInBackground() throws Exception  {
                //Do long running thread work here
                int i = 0;
                while (i++ < 100) {
                    System.out.println(i);
                    Thread.sleep(100);
                }
                return null;
            }

            @Override
            protected void done(){
                // What to do when the long runnng thread is done
                continueButton.setVisible(true);
            }


        };


        //Button to start the long running task
        JButton button = new JButton("Click Me");
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                sw.execute();
                d.setVisible(true);
            }});
        add(button);
    }


    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setContentPane(new TempProject());
                frame.setPreferredSize(new Dimension(500, 400));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }   


}
Community
  • 1
  • 1
Nick Rippe
  • 6,465
  • 14
  • 30
-1

you could use something like this to stop the code for 5 seconds

       try {
            Thread.sleep(5000); // do nothing for 5000 miliseconds (5 seconds)
        } catch (InterruptedException e) {
            e.printStackTrace();
       }
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
Peerkon
  • 65
  • 4
  • I only want to display the button of this JOption pane after 5 sec, not to stop the whole code. – user1633807 Aug 29 '12 at 22:26
  • 2
    Blocking the EDT for 5 seconds will indeed make sure the user cannot dispose of the window. It will probably also make sure you cannot read the contents of the dialog (or other nasty side-effects). One should **never** block the EDT – Robin Aug 30 '12 at 05:58