Results 1 to 6 of 6
- 03-29-2011, 08:29 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Swing GUI With Progress Bar Threaded Joins
Hi all
i am new to java and this forum hope i have this posted in correct place...
i have a GUI when a button is pushed it will zoom off with a progress bar, that all works fine, the problem is when i put more tasks to do in the same action it will stop the progress bar from showing when i use .join???
when i use the join it waits for the thread to complete then start next thread which i do want but when run the progress bar remains @0 untill that thread completes then starts the second without showing or updateing the progress bar 1...
any help will be great.. thanks
FINE=
NOT WORKING=Java Code:Button 1 Actionlistner= demuxThread startDemuxThread = new demuxThread(); startDemuxThread.start();
Java Code:demuxThread startDemuxThread = new demuxThread(); startDemuxThread.start(); try { startDemuxThread.join(); } catch (InterruptedException ex) { Logger.getLogger(EncodingForm.class.getName()).log(Level.SEVERE, null, ex); } demuxThread2 startDemuxThread2 = new demuxThread2(); startDemuxThread2.start();
Thread Class =
Java Code:class demuxThread extends Thread { public demuxThread() { } public void run() { while(true) { Progress frame = new Progress(); frame.pack(); frame.setSize(190, 60); frame.setVisible(true); try { Runtime r = Runtime.getRuntime(); String cmd[] = { "ping" , "www.google.com" }; Process p = r.exec(cmd ); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String lineRead =null; StringBuffer foo = new StringBuffer(""); outputArea.setText("initializing..."); demuxProgressBar.setIndeterminate(true); while( (lineRead = reader.readLine() ) != null) { lineRead = lineRead.trim(); if(lineRead.contains("--")){} else { foo.append(lineRead).append("\n"); } outputArea.setText(foo.toString()); System.out.println(lineRead); demuxProgressBar.setIndeterminate(true); } } catch(Exception e) { e.printStackTrace(); } demuxProgressBar.setIndeterminate(false); demuxProgressBar.setValue(100); demuxLabel.setForeground(Color.green); frame.setVisible(false); return; } } }Last edited by peterhammond; 03-29-2011 at 01:22 PM. Reason: code tags added
-
This makes sense since if you use join you will lose all the advantages of background threading. Please tell us what it is exactly that you are trying to achieve, and perhaps we can give you better suggestions. I can tell you that join is not what you want to do here, and perhaps you wish to use a SwingWorker and add a PropertyChangeListener to it.
A very useful tutorial that will likely help you is this one: Concurrency in Swing
- 03-29-2011, 01:09 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
hi Fubarable
thanks for the link i will look at it and see how i get on....
i would like it to work as such;;
button 1 pressed
MAIN THREAD ---->>>> excuting all code till reach command
demuxThread startDemuxThread = new demuxThread();
startDemuxThread.start();
STOP MAIN THREAD AND RUN startDemuxThread with all progress bars running and and updating info area...
once startDemuxThread has finished RESTART MAIN THREAD and continue rest of action....
am using net beans to speed up building the GUI but not 100% of all actions it has and how to use them to the best...
hope that makes sence..
many thanks
/////////////////////////
Button code could look like this...
Java Code:System.out.println("Please Wait Starting Up Thread"); demuxThread startDemuxThread = new demuxThread(); startDemuxThread.start(); System.out.println("Thread Has Finished Continuing"); System.out.println("Please Wait Starting Up Thread 2"); demuxThread2 startDemuxThread2 = new demuxThread2(); startDemuxThread2.start(); System.out.println("Thread2 Has Finished Continuing");Last edited by peterhammond; 03-29-2011 at 01:23 PM.
-
It sounds as if the code for both "threads" shouldn't be in separate threads at all but rather just code blocks. Then you could call them both sequentially in one background SwingWorker thread.
Also I edited your code posted in your original post and added code tags which should help make your posted code retain its formatting and be more readable.
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luckJava Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 03-29-2011, 02:39 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
ok thanks will give it a go...
atleast i know its not the code thats hanging... its that i is waiting for what i have asked it to do... :)
i will try code in a single thread thanks
- 03-29-2011, 05:01 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
sweet thanks have dumped it all in background thread all on its own..
as i am new when i discover a new thing to add to my project i need to fit it in...
so as u can imagine not very well designed or thought out as i just write in new stuff once i learn about it....
once i have implimented many more items i should hopefully have a good little program that i can use to design a proper one when i move to another...
thanks for all ya help..
Similar Threads
-
Threaded algorithms
By TerTer in forum Threads and SynchronizationReplies: 0Last Post: 04-20-2010, 02:58 PM -
Container-Manager EntityManager - Automatically joins JTA ?
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-05-2009, 01:14 PM -
Progress bar, threads and swing horror
By taptapthat in forum AWT / SwingReplies: 0Last Post: 11-06-2009, 10:40 AM -
joins in sql
By katkamravi in forum JDBCReplies: 5Last Post: 07-23-2009, 11:08 AM -
Minimal single threaded web server?
By johann_p in forum New To JavaReplies: 2Last Post: 04-24-2008, 04:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks