3

My task is necessary and shouldn't be canceled, how do I ask ProgressMonitor not to display the "Cancel" button, so when it finishes, it will auto close the panel.

Frank

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Frank
  • 30,590
  • 58
  • 161
  • 244

2 Answers2

4

I was thinking maybe I can ask it to return the components in it and delete the button

Using the ProgressMonitorDemo from the Swing tutorial (linked to by BalusC) I made the following changes:

public void propertyChange(PropertyChangeEvent evt) {
    if ("progress" == evt.getPropertyName() ) {
        int progress = (Integer) evt.getNewValue();
        progressMonitor.setProgress(progress);

        //  Added this

        AccessibleContext ac = progressMonitor.getAccessibleContext();
        JDialog dialog = (JDialog)ac.getAccessibleParent();
        java.util.List<JButton> components =
            SwingUtils.getDescendantsOfType(JButton.class, dialog, true);
        JButton button = components.get(0);
        button.setVisible(false);

        // end of change

        String message =
            String.format("Completed %d%%.\n", progress);
        progressMonitor.setNote(message);
        taskOutput.append(message);
        if (progressMonitor.isCanceled() || task.isDone()) {
            Toolkit.getDefaultToolkit().beep();
            if (progressMonitor.isCanceled()) {
                task.cancel(true);
                taskOutput.append("Task canceled.\n");
            } else {
                taskOutput.append("Task completed.\n");
            }
            startButton.setEnabled(true);
        }
    }
}

You will need to download the Swing Utils class as well.

The code should only be executed once, otherwise you get a NPE when the dialog closes. I'll let you tidy that up :).

camickr
  • 321,443
  • 19
  • 166
  • 288
1

That's not possible. You can however create a custom progress monitor as outlined in this tutorial.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I was thinking maybe I can ask it to return the components in it and delete the button. – Frank May 06 '10 at 02:50