dsafas fsafasfsa
dsafas fsafasfsa

Reputation: 101

JProgressBar is updated at the end instead of dynamically

The problem is my progress bar updates right after my application stops running. I'm trying to figure out how to do it dynamically while the application runs. After multiple unsuccessful hours of trying I've decided to ask you guys. I have tried adding propertyChange listener without success. My app reads large amount of documents.

It stores 'documentsTotal' which is pretty self-explanatory. Total documents change depending on the extensions selected by user. If user selects .docx only then documentsTotal is going to be 10 and not 60 documents.

'documentsTagged' is the number of document in the queue my app is currently reading. I run the app and it reads those documents in one go. Because of this I have no idea how to make my JProgressBar dynamic.

This is part of my code responsible for the process. It is being run after user presses 'Run' button.

        runBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            progressBar.setValue(0);
            ArrayList<JCheckBox> checkboxes = new ArrayList<JCheckBox>();
            stopBtn.setEnabled(true);
            extensions.clear();
            consoleTextArea.setText("");

            done = false;

            for( Component comp : panel.getComponents() ) {
               if( comp instanceof JCheckBox) checkboxes.add( (JCheckBox)comp );
            }

            for (JCheckBox jcb : checkboxes) {
                if(jcb.isSelected()) {
                    extensions.add(jcb.getText());
                }
            }

            String sql = "SELECT document, file_name, data FROM documents ";

            for (int i = 0; i < extensions.size(); i++) {
                if(i == 0) {
                    sql += "WHERE file_name like '%" + extensions.get(i) + "' ";
                } else if (i >= 1) {
                    sql += "OR file_name like '%" + extensions.get(i) + "' ";
                }
            }

            if(!txtFilename.getText().equals("") && extensions.isEmpty()) {
                sql += "WHERE file_name like '" + txtFilename.getText() + "' ";
            } else if(!txtFilename.getText().equals("")) {
                sql += "AND file_name like '" + txtFilename.getText() + "' ";
            }

            if(!txtrWhere.equals(null)) {
                sql += txtrWhere.getText();
            }

            if(txtFilename.getText().equals("") && extensions.isEmpty()) {
                sql += "WHERE file_name like '%.pdf' OR file_name like '%.doc' OR file_name like '%.docm' "
                        + "OR file_name like '%.dox' OR file_name like '%.xls' OR file_name like '%.xlsx' ";
            }

            sqlParam = sql;

            SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {

                    KeywordsRecognition app = new KeywordsRecognition(sqlParam);

                      int progress = 0;
                      setProgress(0);

                      int documentsTotal = app.getDocumentsTotal();
                      int documentsTagged = app.getDocumentsTagged();

                      progressBar.setMaximum(documentsTotal);

                      while (progress < documentsTotal) {
                            documentsTotal = app.getDocumentsTotal();
                            documentsTagged = app.getDocumentsTagged();
                            progress += documentsTagged;
                            setProgress(progress);
                            progressBar.setValue(progress);
                      }

                    return null;        
                }
            };

            stopBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    worker.cancel(true);
                    stopBtn.setEnabled(false);
                }
            });

            worker.execute();
        }
    });

Upvotes: 0

Views: 405

Answers (1)

LunaVulpo
LunaVulpo

Reputation: 3221

See docs: https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html#publish(V...)

you have to use publish() method to publish progress and handle it in progress() method to update progressbar.

Not tested (only idea):

   SwingWorker<Void, Void> worker = new SwingWorker<Void, Integer>() {
            @Override 
            protected Void doInBackground() throws Exception {

                KeywordsRecognition app = new KeywordsRecognition(sqlParam);

                  int progress = 0;
                  setProgress(0);

                  int documentsTotal = app.getDocumentsTotal();
                  int documentsTagged = app.getDocumentsTagged();

                  progressBar.setMaximum(documentsTotal);

                  while (progress < documentsTotal) {
                        documentsTotal = app.getDocumentsTotal();
                        documentsTagged = app.getDocumentsTagged();
                        progress += documentsTagged;
                        setProgress(progress);  
                        //sent to ui
                        publish(progress);

                  } 

                return null;         
            } 

      @Override
      protected void process(List<Integer> chunks) {
           //here update ui
           for(Integer p : chunks){
                  progressBar.setValue(p);
            }
      }
    }; 

Upvotes: 1

Related Questions