Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Rep Power

    Default How do I close Cancel Dialog Box by code

    I am doing something in the background and I pop a dialog box to allow the user to cancel this background job.

    But when I finish this background task, I want to close the dialog automatically without hitting the lone 'cancel' button in my dialog box.

    I'd appreciate a sample code. Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    the thread in the background needs to have a handle to the dialog object, when it is done, call the dialog's setVisible() method right ?

    this is confusing because both the dialog needs a reference on the thread thing, so the action for pusing the cancel button can cause the thread to be interrupted, you probably have this already right.

    but the thread also needs a reference to the dialog so that it can call the dialog's setVisible() method when the threaded stuff completes.

    for example, maybe something like this would be ran in a thread.
    Java Code:
    class Worker implements Runable {
      JDialog myDialog;  // or the AWT one, I guess.
      public Worker(JDialog waitDialog) {
        this.myDialog = waitDialog;
        // i assume you have already set an instance to this worker in the dialog ?
        // so it knows how to get a hold of this to interrupt it.
      boolean running;
      // this probably exists, and you call it from the dialog to stop right.
      public void doStop() {
        running = false;
      public void run() {
        while (running) {
        // the thread stuff goes here
        // oh, this thread is exiting, 

  3. #3
    Join Date
    Dec 2009
    Rep Power


    Thank you for the response.

    Ok I think I am getting it, sorry I may not have posted enough information.

    This is the code:

    Java Code:
                    String cancel = "" + JOptionPane.showOptionDialog(mainPanel,
                            "This make take several minutes depending on your internet connection", "Refreshing tree...",
                            JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[] { "Cancel" }, null);
    From the way I understand it is I need to subclass my own JDialog box so I can have better control like hiding the window as you suggested.

Similar Threads

  1. Struts 2 cancel button
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 09-29-2009, 04:40 PM
  2. Cancel Particular scheduled task
    By hjava in forum New To Java
    Replies: 0
    Last Post: 08-12-2009, 12:10 AM
  3. How should I cancel this thread ?
    By playwin2 in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-27-2008, 07:20 PM
  4. Handlinh pdf open ,save,cancel in jsp
    By sunkaras in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2008, 10:08 AM
  5. Disable BACK and CANCEL buttons in wizard
    By consutes in forum SWT / JFace
    Replies: 2
    Last Post: 11-12-2007, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts