0

How can I add a progressbar that would show how much time will it take untill the current process in done?
I do know how to construct and use a progressbar, in simple ways, but I do not know how to bound them with a process.

For example when I click a button, I would like to open a jfilechooser, and this may take some time. I want the user to know that the app did not crash and to give an idea of how much time will it take until the jfilechooser appears.

Thanks in advance

3
  • 1
    Opening jfilechooser takes a lot of time? Or do you mean opening the actual file and processing it in background thread? If the latter, simply update JProgressBar once in a while from that thread (remember about thrad safety). BTW please tag your question with swing. Commented Nov 25, 2012 at 13:12
  • 1
    SwingWorker seems like a right choice here. Commented Nov 25, 2012 at 13:22
  • @Tomasz Nurkiewicz jfilechooser was only an example, because at the moment, I do have a problem with it, takes up to 30 sec to open at the first button click.Then at the second try, drops to 3 sec. But anyway I needed to assign a progressbar to my program because it's a large one with 3d graphics Commented Nov 25, 2012 at 13:32

1 Answer 1

1

The thing is, when dealing with classes you didn't write, it's not that easy to link the progress bar with the actual progress.

If you'd like to bind it with a progress of some task, here are the steps:

  1. Assign a thread to the time-consuming task
  2. Use a static field in the parent class as the progress you've accomplished in your task. (may be counter from 1 to 100)
  3. Every specific period (1 sec) you check that static field and set the progress bar with it.
  4. Inside your task, you divide your long process into solid steps (i.e. opening a file, processing, preparing extracted data ...etc).
  5. After every solid step, update the static field in the parent class with the progress so far.

Make sure to regulate the process of writing and reading the static field using semaphores.

Sign up to request clarification or add additional context in comments.

2 Comments

Will be a difficult job, but I think I got the idea. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.