2

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?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238

4 Answers4

3

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • Yes, it's possible: http://stackoverflow.com/questions/2778045/how-to-omit-the-cancel-button-in-java-progressmonitor – Barbe Rouge Nov 04 '14 at 13:29
  • @BarbeRouge that answer you're pointing to says the same thing mine does - you can't use ProgressMonitor, you have to make your own JProgressBar dialog. The only difference is that they copied the code from the Java Tutorial into the answer. – Paul Tomblin Nov 04 '14 at 13:43
3

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
  • 6,562
  • 2
  • 38
  • 47
1

I know this post is a little old, but the first answer on google, so i'll post my answer...

I've no solution to disable the cancel button but I use this tip: redisplay the progress monitor when cancel is clicked.

public void propertyChange (PropertyChangeEvent event) {
    if (event.getPropertyName().equals("progress")) {
        int progress = ((Integer) event.getNewValue()).intValue();
        lastProgress = progress;
        progressMonitor.setProgress(progress);
    }
    if (progressMonitor.isCanceled()) {
        progressMonitor = new ProgressMonitor(panel, "", "", 0, 100);
        progressMonitor.setMillisToDecideToPopup(0);
        progressMonitor.setMillisToPopup(0);
        progressMonitor.setProgress(lastProgress);
    }
}

Of course you'll have to remember the last progress value.

This solution is a bit uggly, but it works for me.

Yves

Yves H
  • 13
  • 2
0

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
  • 188,989
  • 46
  • 291
  • 292