0

The Find() method is a sample of my method that makes a search of a word into some files. I call it from a Button_Start mouseclicked event.
Here is my code:

public void Find (String word) {

   List_files_directories (directory.getAbsolutePath(), files);

// print list  
    textpane.append(java.awt.Color.cyan, "Files in the choosen directory\n");

for (int j=0; j<files.size(); j++) { 
       if (files.get(j) != null)
                 textpane.append( java.awt.Color.PINK, "\n" + files.get(j) );
   }

// from listarray to array
    String[] array_files = new String[files.size()];
    array_files = files.toArray(array_files);

int j;        
for (j=0; j<array_files.length; j++) {
if (array_files[j] != null) {   

        if (array_files[j].contains("1")) {
            function1 (  array_files[j], word );
         } 

        else if (     array_files[j].contains("2")   ) 
        {
        function2 (  array_files[j], word);
        } 

        else if (     array_files[j].contains("3")    ) 
       {
        function3 (   array_CARTELLE[j], word  );
        } 

       }            
}        
} 

private void Button_StartMouseClicked(java.awt.event.MouseEvent evt) {                                           
        java.util.Timer().schedule( 
        new java.util.TimerTask() {
            @Override
            public void run() { 
                    Find(word_from_textfield); 
            }
        }, 
        10
);
}

I need to add 2 things:

  1. I'd like to make a JProgressBar while Find() method is running.
    In this case I need to adopt a standard to set set the bar progress i.e. based on the files list length, I don't know.
  2. I'd like to set WAIT_CURSOR while Find() method is running, so until progress bar reaches 100%.

I tried this for my request 1):

        public void doWork() {

            Worker worker = new Worker();
            worker.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if ("progress".equals(evt.getPropertyName())) {
                        ProgressBar1.setValue((Integer) evt.getNewValue());
                    }
                }
            });

           worker.execute();
        }

   public class Worker extends SwingWorker<Object, Object> {     
            @Override
            protected Object doInBackground() throws Exception {

                for (int index = 1; index <= 1000; index++) {
                    int progress = Math.round(((float) index / 1000) * 100f);
                    setProgress(progress);

                    Thread.sleep(100);
                }
                return null;
            }
        }

In this I call doWork() at the beginning of my Find() method. The problem is that I don't know how to synchronize my method duration and the progress bar length. Besides I don't know if it's the best way to reach my aim.

Could you please help me giving an example according to my code/situation?

Thanks

edit: here is my goal: My method prints on a JTextPane ( textpane.append() ) all files from the user directory, after that it search a word in each file. I want to use a JProgressBar while it's printing on the JTextPane and it has to be synchronized with the process duration. If it's possibile the print has to be timed: Example:

// Set Mouse cursor to Wait Status
// execute Find():
// the JProgressBar has to follow this progress of the print on JTextPane.
Files in the choosen directory:
file1
// print on JTextPane after 1 second
file2
// print on JTextPane after 1 second
file3
// print on JTextPane after 1 second

word found in file1!
// print on JTextPane after 1 second
word not found in file2
// print on JTextPane after 1 second
word found in file3
// print on JTextPane after 1 second
// restore mouse cursor to default
Frank
  • 2,083
  • 8
  • 34
  • 52
  • 1
    please not attack, don't undestand, maybe code is different as description, please whats goal, maybe JTextPane, could be Jlist or JTable instead, or ???, maybe I'm wrong and missread something – mKorbel May 20 '13 at 13:00
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 20 '13 at 13:05

2 Answers2

2

I think you need to integrate the work done by Find() into the SwingWorker. Then you could set your progress bar after processing each file.

@Override
protected Object doInBackground() throws Exception {

    for (int j=0; j<files.size(); j++) { 
       if (files.get(j) != null)
                 textpane.append( java.awt.Color.PINK, "\n" + files.get(j) );
    }

    // from listarray to array
    String[] array_files = new String[files.size()];
    array_files = files.toArray(array_files);

    int j;        
    for (j=0; j<array_files.length; j++) {
        if (array_files[j] != null) {   

            if (array_files[j].contains("1")) {
                function1 (  array_files[j], word );
            } 

            else if (     array_files[j].contains("2")   ) 
            {
                function2 (  array_files[j], word);
            } 

            else if (     array_files[j].contains("3")    ) 
            {
                function3 (   array_CARTELLE[j], word  );
            } 

        // THIS IS THE NEW BIT
        setProgress((int)(100 * (float) j / files.size()));
    }
} 
Tom McIntyre
  • 3,620
  • 2
  • 23
  • 32
  • sorry, what's "publish"? I don't understand what I have to do – Frank May 20 '13 at 13:35
  • publish is a protected method on SwingWorker...but in this case you actually want to use setProgress, another protected SwingWorker method. I have updated my answer. Your listener should now pick up these progress events. – Tom McIntyre May 20 '13 at 14:02
  • +1 for `setProgress()`, which may be called on the background thread; I've elaborated on `publish` [here](http://stackoverflow.com/a/16652962/230513). – trashgod May 20 '13 at 15:33
2

What's publish()? I don't understand what I have to do.

The methods publish() and process() let the SwingWorker reliably communicate interim results from the background thread to the event dispatch thread (EDT). In this example, process() displays the current result of a long-running calculation; in this related example, process() also updates a chart's data model.

Addendum: As @mKorbel notes, you can leverage generic type checking by using a more specific type, e.g. SwingWorker<Double, Double>.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    +1 but not sure if declare SwingWorker is proper of ways, still missing reason of this question, (leaving that for whales) – mKorbel May 20 '13 at 16:13