|
|||
|
Hi,
I want to display the progress of a task to the user. The task happen in a non gui class of my application which simply update a currentProgress attribute which I want the Progress Bar class to check every second and update the bar. I have the issue that the dialog containing the JProgressBar displays but none of the components are displayed until the task has completed. Debugging the code I discovered that the timer event which should fire every second also does not until the task is over. I am sure I am doing something fundamentally wrong but have searched newsgroups and the net with no luck. I have also looked at and implemented the Java Sun Progress Bar examples. Below is the code I have on my main code line: Code>>>>>>>>>>>>>>>>>>>>>>> //First we create the ProcessBarDialog but do nothing with it at this stage. final ProgressBarDialog pbd = new ProgressBarDialog("Paste", false); SwingWorker bar = new SwingWorker(){ public Object construct() { pbd.setProgressable(task); //Display and start the ProgressBarDialog pbd.startProgress(); return pbd; } }; bar.start(); //Actual Task Runs on main line so we don't return until it is complete java.util.List itemsToRemove = new ArrayList(); for (int i=0; i < pastedObjects.size(); i++) { if (pastedObjects.get(i) instanceof AdvancedGraph) { itemsToRemove.add(pastedObjects.get(i)); } } pastedObjects.removeAll(itemsToRemove); .. . . .. . . .. . . CONT... END CODE>>>>>>>>>>>>>>>>>>> The ProgressBarDialog class is simple a dialog class which displays a JLabel, JProgressBar and JButton. pbd.setProgressable(task); This stored a reference in the Dialog class so it can get the current progress of the task every time the Timer event fires. Below is the contents of the pbd.startProgress(); method: CODE>>>>>>>>>>>>>>>>>>>> timer = new Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { //Until the task is initalised we can not do anything if(task != null){ //Get the current progress. int currentProgress = task.getCurrentProgress(); progressBar.setValue(currentProgress); if (currentProgress > 0 && progressBar.isIndeterminate()) { progressBar.setIndeterminate(false); progressBar.setString(null); //display % string } if (task.isDone() ) {//|| cancelled timer.stop(); progressBar.setValue(progressBar.getMinimum()); progressBar.setString(""); //hide % string parentFrame.dispose(); //reset the task ready for use again if required. task.reset(); } } } }); //Display the Progress Bar dialog displayProgress(); //Set the mode to Indeterminate to start with. progressBar.setIndeterminate(true); //Start the timer. timer.start(); END CODE>>>>>>>>>>>>>>>>>>>>>>>> The displayProgress() method creates a JDialog and puts the components in the Dialog. I have tried invokeLater and invokeAndWait in a number of places and the whole thing it really starting to annoy me. Thanks for anybodys help. James |
|
|
||||
|
||||
|
|
|
|||
|
On Tue, 02 Aug 2005 14:01:44 GMT, James Gralton wrote:
> I have tried invokeLater and invokeAndWait in a number of places and the > whole thing it really starting to annoy me. Perhaps you should try it in your SSCCE. (I doubt anybody will wade through that code snippet mess you posted.) Of course, you had a look through the GUI FAQ, and ruled out blocking the EDT? -- Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info See You On Some Other Channel |
|
|||
|
Thanks,
I thought it better to format my question the way I did rather than dump all my code. I tried to get all the information people would need to help in in the best way possible and don't believe if you read it properly it is a mess. Andrew Thompson wrote: > On Tue, 02 Aug 2005 14:01:44 GMT, James Gralton wrote: > > >>I have tried invokeLater and invokeAndWait in a number of places and the >>whole thing it really starting to annoy me. > > > Perhaps you should try it in your SSCCE. > (I doubt anybody will wade through that code > snippet mess you posted.) > > Of course, you had a look through the GUI FAQ, > and ruled out blocking the EDT? > |
|
|||
|
James Gralton wrote:
> I tried to get all the information people would need to help in in the > best way possible and don't believe if you read it properly it is a mess. In case you are interested in a second opinion. Your random selection of incoherent fragments is a mess. I started to look at the code, recognized that it was just fragments, in no way indicating the real flow of control and events, and that I would probably have to make a drawing to piece it all together. At that point I decided to skip it. No one here has an obligation to help you. If you want help it would be clever to present the problem in a way that is easy to comprehend. If you can't be bothered to do that, why should we bother to spend out time on your problem? /Thomas -- The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq http://www.uni-giessen.de/faq/archiv....java.gui.faq/ |
|
|||
|
Sorry Thomas/Andrew,
I thought I had made an ok post but obviously in my frustration with this issue rushed it a but. I do really appreciate yours and everyones help. I have constructed a small example which compiles and runs to demonstrate my issue. It contains 4 classes to demonstrate the set up I am trying to achieve. One of the classes is the SwingWorker class from Sun's Java site. The src is at www.gralton.com/post/src and the classes if you needs them are at www.gralton.com/post/classes The main class is Simple. Upon running the app please press the Go Task button. The ProgressBarDialog displays with no JProgressBar. Thanks for your help here. James Thomas Weidenfeller wrote: > James Gralton wrote: > >> I tried to get all the information people would need to help in in the >> best way possible and don't believe if you read it properly it is a mess. > > > In case you are interested in a second opinion. Your random selection of > incoherent fragments is a mess. I started to look at the code, > recognized that it was just fragments, in no way indicating the real > flow of control and events, and that I would probably have to make a > drawing to piece it all together. At that point I decided to skip it. > > No one here has an obligation to help you. If you want help it would be > clever to present the problem in a way that is easy to comprehend. If > you can't be bothered to do that, why should we bother to spend out time > on your problem? > > /Thomas > |
|
|||
|
James Gralton wrote:
> > I am sure I am doing something fundamentally wrong but have searched > newsgroups and the net with no luck. I have also looked at and > implemented the Java Sun Progress Bar examples. In addition to what other people have said, if you think the problem is in a certain area but can't find it there, perhaps it is elsewhere. To do any effective diagnosis we need a small, self-contained example that illustrates the problem without extraneous details. If you do find the problem, do tell us what it was. It may help someone searching through the archives for a similar problem, and makes us more aware of the sort of problems that other people have. Anyway here are some things you might want to consider. > final ProgressBarDialog pbd = new ProgressBarDialog("Paste", false); Made up acronyms don't help readablitiy. > SwingWorker bar = new SwingWorker(){ > public Object construct() { What does this method do? The latest SwingWorker documentation is here https://swingworker.dev.java.net/jav...ingWorker.html The method isn't present. I know everyone likes to support out of date machines, but it's well worth upgrading to the current JRE/JDK and slapping on an @Overrides. A liberal sprinkiling of assert [!]EventQueue.isDispatchThread(); would help to document in which thread blocks should be run (don't forget -ea). Tom Hawtin -- Unemployed English Java programmer http://jroller.com/page/tackline/ |
|
|||
|
Unfortunatly I can get the example to work in smaller programs it is when I try to extend it to my
application that I am have the issue below. I know 4 classes is quite a lot ofr a small example but each of them is doing very little. If I find a solution I will reply to the post for sure. Thanks for your advice I will look at the new SwingWorker documentation and see if it helps me at all. James Thomas Hawtin wrote: > James Gralton wrote: > >> >> I am sure I am doing something fundamentally wrong but have searched >> newsgroups and the net with no luck. I have also looked at and >> implemented the Java Sun Progress Bar examples. > > > In addition to what other people have said, if you think the problem is > in a certain area but can't find it there, perhaps it is elsewhere. To > do any effective diagnosis we need a small, self-contained example that > illustrates the problem without extraneous details. > > If you do find the problem, do tell us what it was. It may help someone > searching through the archives for a similar problem, and makes us more > aware of the sort of problems that other people have. > > > Anyway here are some things you might want to consider. > >> final ProgressBarDialog pbd = new ProgressBarDialog("Paste", false); > > > Made up acronyms don't help readablitiy. > >> SwingWorker bar = new SwingWorker(){ >> public Object construct() { > > > What does this method do? The latest SwingWorker documentation is here > > https://swingworker.dev.java.net/jav...ingWorker.html > > > The method isn't present. I know everyone likes to support out of date > machines, but it's well worth upgrading to the current JRE/JDK and > slapping on an @Overrides. > > A liberal sprinkiling of assert [!]EventQueue.isDispatchThread(); would > help to document in which thread blocks should be run (don't forget -ea). > > Tom Hawtin |
|
|||
|
James Gralton wrote:
> > I have constructed a small example which compiles and runs to > demonstrate my issue. It contains 4 classes to demonstrate the set up I > am trying to achieve. One of the classes is the SwingWorker class from > Sun's Java site. It's still far from minimal, but no matter. Just looking at the code without running it. public static void main(String[] args) { try{ UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName()); Simple s = new Simple(); s.setVisible(true); } catch(Exception e){ e.printStackTrace(); } } This needs to be done in the EDT. Wrap it in: EventQueue.invokeLater(new Runnable() { public void run() { }}); From goTask in the EDT: SwingWorker bar = new SwingWorker(){ public Object construct() { pbd.setTask(t); //Display and start the ProgressBarDialog pbd.startProgress(); return pbd; } }; bar.start(); t.doJob(); And doJob has a sleeping while loop. That loop is running in the EDT. Therefore no events can run and the GUI is locked up. If we look in ProgressBarDialog.startProgress it is doing stuff to GUI components from outside of the EDT. That should not be done. I suggest plenty of assert EventQueue.isDispatchThread(); and assert !EventQueue.isDispatchThread(); (and -ea). Actually I suggest wrapping EventQueue.isDispatchThread/invokeLater/invokeAndWait in an interface so that you can do unit testing. Tom Hawtin -- Unemployed English Java programmer http://jroller.com/page/tackline/ |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dataset - Updating - killed the process | chandu.isi@gmail.com | Newsgroup comp.soft-sys.sas | 2 | 07-16-2006 11:45 PM |
| Re: Updating missing values ?? | Venky Chakravarthy | Newsgroup comp.soft-sys.sas | 0 | 06-13-2006 09:08 PM |
| Re: updating a dataset using MODIFY | Jairaj Sathyanarayana | Newsgroup comp.soft-sys.sas | 0 | 05-28-2005 10:11 PM |
| Re: creating a date variable displaying all days in year | Schwarz, Barry A | Newsgroup comp.soft-sys.sas | 0 | 04-26-2005 10:52 PM |
| Re: updating database in oracle | Harry Droogendyk | Newsgroup comp.soft-sys.sas | 1 | 01-28-2005 02:31 PM |