DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2006
    Posts
    8

    JProgressBar and Thread problem

    hi
    im trying to make a GUI for a HTML parser
    The problem is JProgressBar is not showing the progress, only updates when the
    total have been downloaded.
    I have been reading and the problem seems to be the Main thread is bloked by the actionperformed so i did try to convert the personalParser into a new Thread but still didnt work
    the basic structure of my app is

    Code:
    class gui{
        JProgressBar preloader;
        JButton download;
        JLabel  display;
          //other components .....
        public gui(){
             preloader=new JProgressBar ();
             download=new JButton ("Download");
             display=new JLabel  ("");
             myButtontListener  mbl=new myButtontListener  (preloader);
             download.addActionListener(mbl);
             //frame.add etc .......
        }
    }
    class myButtontListener  implements ActionListener{
        JProgressBar preloader;
        personalParser myParser;
        JLabel  display;
       public myButtontListener  (JProgressBar bar, JLabel lbl){
        preloader;=bar;
        display=lbl;
       }
        public void actionPerformed(ActionEvent event) {
          if( event.getActionCommand() == "Download"  ){    
               myParser=new personalParser ("http://server.com/", preloader);
               String [] HtmlCode=myParser.getContentInArray();
               display.setText(HtmlCode[5]);
          }
    
        }
    }
    
    class personalParser {
        JProgressBar preloader;
        String url
        public personalParser (String url,JProgressBar bar ){
          preloader=bar;
          this.url=url;
          
        }
    
        public String[] getContentInArray(){
           String [] code=new String[100];
           //some net connections ........
    
           //here i use a loop to fill the array and need to set the JProgressBar value
          //to display how many blocks of a especific tag have been downloaded and
          //added to the array
           String TagBlock="";
           int count=0;
           while ((TagBlock = input.readLine()) != null) {
               count++;
               code[count]=TagBlock;
               preloader.setValue ( count );
           }
           return code
        }
     
    }
    How can i fix this ?

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    One approach is to use a Runnable interface. Take this class for example:
    Code:
    
    public class JProgressBarDemo extends JApplet implements Runnable {
        private JProgressBar progressBar = new JProgressBar();
        
        public void init() {
            new Thread(this).start();
        }
    
        public void run() {
            while (progressBar.getValue() < progressBar.getMaximum()) {
                progressBar.setValue(progressBar.getValue() + 1);
            }
        }
    }
    
    That can also be written as:
    Code:
    
    public class JProgressBarDemo extends JApplet {
        private JProgressBar progressBar = new JProgressBar();
        
        public void init() {
            new Thread(new Runnable() {
                public void run() {
                    while (progressBar.getValue() < progressBar.getMaximum()) {
                        progressBar.setValue(progressBar.getValue() + 1);
                    }
                }
            }
        }
    }
    

  3. #3
    Join Date
    Jan 2006
    Posts
    8
    thanks destin
    that really help me.
    Now my parser implements Runnable then when the parsing is complete i kill the thread and the progress bar is updating correctly.

Similar Threads

  1. Replies: 0
    Last Post: 12-26-2001, 04:48 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links