Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Progress Dialog 1

Status
Not open for further replies.

Miros

Programmer
Jan 27, 2001
506
US
I'm sure someone's asked this before, but I can't find it.

I have a long, time-consuming process. I want to put up a dialog which will a) block access to the controls in the main window, b) allow users to click Cancel, and c) allow my process to run smoothly.

In VC++, this was called a modeless dialog. From other posts I've read here, a Java modeless dialog doesn't do a). From what I can tell, a modal dialog won't do b) and c) simultaneously.

Can someone clarify terms and point me in the right direction?

Rose/Miros



 
Have you looked at JProgressBar ?

You need to give it a range I believe of "how long the process will take" - which in itself can be a nightmare depending on what you are doing. But, I believe the idea is that you :

Fire a JProgressBar
Fire a Thread to do the work
Update the JProgessBar's indicators to let the user see how the process is doing ...

Some simple examples :
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Since I'm processing all the files in a directory, File.list() will tell me how many files I'm working with.

I think even if it has to recurse, it will be fairly smooth to simply add the number of files in a subdirectory to the progress bar before updating the "done" counter, assuming we're not talking about 2 subdirectories with 1000 files each!

 
Ok, here's my current code...

Code:
    public String addItemsFromURL( final String url, final JTextField textfield )
    {
    	Object[] options = { "Cancel" } ;
    	final JOptionPane optionPane = new JOptionPane(
		    "Slow process running. Please wait!",
		    JOptionPane.DEFAULT_OPTION,
		    JOptionPane.INFORMATION_MESSAGE,
		    null,     //don't use a custom Icon
		    options,  //the titles of buttons
		    options[0]); //default button title
		    
		final boolean cancel = false ;

		final SwingWorker worker = new SwingWorker() {
	
	        public Object construct() {
                        // long time consuming process
		    	optionPane.setVisible( false ) ;
			return null ;
		
	        }
	
	        //Runs on the event-dispatching thread.
	        public void finished() 
	        {
	        }
	    };
	worker.start(); 
        optionPane.setVisible( true ) ;
        cancel = true ;

    	return null ;    	
    }

I think this actually works faster than before I spun it off in a thread, but the dialog box isn't visible.

Dialog box is supposed to look like this:
+-------------------------------------+
| Slow process running. Please wait. |
| [Cancel] |
+-------------------------------------+

If the user clicks Cancel, the thread is supposed to stop. If the thread finishes first, it's supposed to hide the dialog.

Is there a different routine I'm supposed to use than "setVisible"?

 
Got it! Stops the back thread when you click Cancel and closes the dialog when it finishes normally.

Code:
    private boolean cancel = false ; 
    // has to be here so it can be set from both addItemsFromURL and the other thread

    public String addItemsFromURL( final String url, final JTextField textfield )
    {
    	Object[] options = { "Cancel" } ;
    	JOptionPane optionPane = new JOptionPane(
		    "Slow process running..",
		    JOptionPane.DEFAULT_OPTION,
		    JOptionPane.INFORMATION_MESSAGE,
		    null,     //don't use a custom Icon
		    options,  //the titles of buttons
		    options[0]); //default button title 
	final JDialog dialog = optionPane.createDialog( textfield, "Please wait" );
		
	cancel = false ;
		    
	final SwingWorker worker = new SwingWorker() {
	
	    public Object construct() 
            {
	        // long time consuming process 
                // which seems faster as a separate thread
	        dialog.setVisible( false ) ;
		return null ;
		
	    }
	
	    //Runs on the event-dispatching thread.
	    public void finished() 
	    {
	    }
	};
	    
	worker.start() ; 
     	dialog.show() ;
	cancel = true ;

    	return null ;    	
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top