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:
JProgressBar
while Find()
method is running.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
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>
.