tags:

views:

1096

answers:

3

I'd like to make use of ProgressMonitor's functionality but I don't want to give the user the option of canceling the action. I know if I don't call isCanceled() then the button press has no effect, but I'd prefer not to have the user believe the program is unresponsive.

How shall I go about doing this?

+3  A: 

You can't. Make your own dialog using a JProgressBar, as described in The Java Tutorial.

Paul Tomblin
A: 

First question is why don't you want the user to be able to cancel the action? If it takes that long, why shouldn't they be able to decide that they don't want to wait for it?

If you really don't want the cancel button, you'll have to bite the bullet and create your own progress dialog. You could look at the ProgressMonitor source to see how they do it (they use a JOptionPane for the dialog and pass it a couple of Strings and a JProgressBar).

Michael Myers
+2  A: 

I don't know why all ProgressMonitor fields are private. Probably not Mr. Godsling's proudest creation :)

 * @author James Gosling
 * @author Lynn Monsanto (accessibility) 
 * @version 1.37 04/12/06

Just clone it, along with some package private stuff from Swing, then you can do whatever you want, like add a flag for cancelability, and use it in ProgressOptionPane's constructor.


(UPDATE) If you can't derive JDK code under SCSL, then here is a devious way to get hold of the JDialog, then you can do anything you want, including removing the Cancel button:

progressMonitor = new ProgressMonitor(ProgressMonitorDemo.this,
                                     "Running a Long Task",
                                     "", 0, 100);
progressMonitor.setMillisToDecideToPopup(0);
progressMonitor.setMillisToPopup(0);
progressMonitor.setProgress(0);
JDialog dialog = (JDialog)progressMonitor.getAccessibleContext().getAccessibleParent();
JOptionPane pane = (JOptionPane)dialog.getContentPane().getComponent(0);
pane.setOptions(new Object[]{});

It's ugly, and it's totally dependent on ProgressMonitor's implementation. So you should check for ClassCastException and null.

Why do you need to set both time periods to 0? Otherwise the dialog is not created in setProgress.

Geoffrey Zheng