Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suggestions on making a progress bar/waiting message

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys

I've got this program which saves data into TXT files and it takes very long to do so. The problem is not how long it take though, the more annoying problem is that everything in my program is blocked until it's done saving and doesn't notif the user of what's going on.... What I mean is I want a message (i was thinking a little window) to appear where saying "Saving..." and disappearing when the program is done saving.

Which SWING class would the best to do this? I was thinking of using JOptionPane but those windows come with buttons... which is unnecessary

Must I create an extension of the JFrame class or is there an alternative?

Any suggestion will be much appreciated

Thank-you

P.S. I was also thinking of adding a progress bar and I could
 
Rudolf Meerkotter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A progress bar would make sense
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> the more annoying problem is that everything in my program is blocked until it's done saving and doesn't notif the user of what's going on....

if you're doing this via actionPerformed() or similar listener, you're probably blocking the swing thread (EDT),
in which case it would be better to save the data via a separate thread, thus leaving your GUI 'responsive',
then perhaps you wouldn't need a progress bar, as the data would be being saved in the background
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case you should definitely check out javax.swing.SwingWorker
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh there's a class called JProgressBar... logically that's what I'm looking for.

I've checked out the SwingWorker class but we're not allowed to use this since it's part of Java 6 (and this project is meant to be coded in Java 5). What is the best alternative?

So far my efforts reveal a problem which seems to be what Michael Dunn pointed out:

I have an algorithm that looks somewhat like this:The problem with this is that the bar doesn't move as the data is being saved. Instead it just magically goes from 0 to 100 when the process is done (which through my understanding seems to be a Thread problem is everything else works when I test it on its own). So to try get around this problem I changed my algorithm a bit to something like:But I still get a similar problem....

I pressume I need to take my first algorithm and run it on a separate Thread but I don't understand why my second algorithm doesn't work :S could anyone please explain this.

Thank-you

 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure that SwingWorker was a class that appeared in tutorials and web articles going back several years, and it was only inducted into Java recently. So if you could track down one of those articles you could get the code for it and use it in your project (legalisms permitting, of course).
 
Michael Dunn
Ranch Hand
Posts: 4632
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic