2

I don't know how to do that, because ProgressMonitor window is self-invoked (after 2 seconds, if necessary) and I don't have any control when and if it will open. Therefore I don't have a clue how to make it open in background, so the focus stays on the main JFrame.

MyFile file = panel.getFilesystem().getFile(panel.getDirectory()+fileName);
if(file.isDirectory()){
    final ProgressMonitor monitor = new ProgressMonitor((Component)event.getSource(), 
            "Determining the size of "+file.getName(), "Initializing...", 0, 100);
    monitor.setMillisToPopup(500);
    monitor.setMillisToDecideToPopup(200);
    final DirectorySizeWorker worker = new DirectorySizeWorker(file, table, monitor, table.getSelectedRow(), 2);
    worker.execute();
    worker.addPropertyChangeListener(
             new PropertyChangeListener() {
                 public  void propertyChange(PropertyChangeEvent event) {
                     if ("progress".equals(event.getPropertyName())) {
                         monitor.setProgress((Integer)event.getNewValue());
                         monitor.setNote((Integer)event.getNewValue() + "% completed");
                     }
                     if("state".equals(event.getPropertyName())){
                         monitor.close();
                     }
                 }
             });
}

monitor is monitoring behaviour of worker. worker is a SwingWorker implementation trying to deremine the file size (in this particular case file is a directory, that's why I assume it may take a long time and use a SwingWorker), if it takes too long monitor invokes a new window with progress bar and notification about the progress. Problem is that this window is focused on and I would rather like it to be opened in background, so the user can still browse files (the program is a simple file manager).

Wojtek
  • 2,514
  • 5
  • 26
  • 31
  • 1
    Let me get the magic 8 ball. You've posted no code and no in-depth description of your application structure and yet we're expected to know what you mean. We're not psychic - please post a section of your code demonstrating your actual issue. – mcfinnigan Feb 28 '12 at 13:57

1 Answers1

3

ProgressMonitor(parentComponent, message, note, min, max)

Where:

parentComponent - the parent component for the dialog box

The dialog will have focus when it is visible, then should return focus to the parent when dismissed or set invisible.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • And it works this way now, the dialog has focus when visible and the main window is focused when dialog is closed (either by clicking "cancel" button or automatically by `monitor.close()` after the `worker` is finished). My point is: how to make it this way, that the dialog is never focused (unless the user selects it on the taskbar)? – Wojtek Feb 28 '12 at 14:50
  • OK, but besides a few convenience methods & a button to close it (for which you would want *focus*), what is the advantage of using a `ProgressMonitor` over (for example) a `JProgressBar` in the main UI that is set visible/invisible according to need? – Andrew Thompson Feb 28 '12 at 15:17
  • Well, the first advantage was obviously simplicity, merely a few lines of code were sufficient. Secondly, I prefer opening it in separate windows, because I can hold each task's progressbar separate, if I kept them in the main UI it could get cluttered because of the large number of progressbars (eg. if the user would like to run a lot of copy-paste tasks parallelly). I'm afraid that this simplicity could've turned against me and eventually I'll be forced to create my own dialog window. – Wojtek Feb 28 '12 at 15:25
  • *"the main UI it could get cluttered"* How about a 'JList` of progress bars (remove each when done)? – Andrew Thompson Feb 28 '12 at 15:30
  • Well, that's another possibility, maybe even better than separate windows. I think I'll do it this way, the UI will stay more compact. Thanks for the suggestions. Anyway, if you would manage to find the answer to my problem, post it here, I'm just curious if it's possible. – Wojtek Feb 28 '12 at 15:36