Results 1 to 6 of 6
  1. #1
    peterhammond is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default 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=
    Java Code:
    Button 1 Actionlistner= 
            demuxThread startDemuxThread = new demuxThread();
            startDemuxThread.start();
    NOT WORKING=
    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 02:22 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  3. #3
    peterhammond is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    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 02:23 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck

  5. #5
    peterhammond is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  6. #6
    peterhammond is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  1. Threaded algorithms
    By TerTer in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-20-2010, 03:58 PM
  2. Container-Manager EntityManager - Automatically joins JTA ?
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-05-2009, 02:14 PM
  3. Progress bar, threads and swing horror
    By taptapthat in forum AWT / Swing
    Replies: 0
    Last Post: 11-06-2009, 11:40 AM
  4. joins in sql
    By katkamravi in forum JDBC
    Replies: 5
    Last Post: 07-23-2009, 12:08 PM
  5. Minimal single threaded web server?
    By johann_p in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •