メソッドは、いくつかのファイルで単語を検索する私のFind()
メソッドのサンプルです。Button_Start
mouseclicked イベントから呼び出します。
これが私のコードです:
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
);
}
2 つのことを追加する必要があります。
JProgressBar
whileFind()
メソッドを実行したいと思います。
この場合、バーの進行状況を設定するための標準を採用する必要があります。つまり、ファイルリストの長さに基づいていますが、わかりません。WAIT_CURSOR
メソッド実行中に設定したいFind()
ので、プログレスバーが100%になるまで。
私は私の要求のためにこれを試しました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;
}
}
これで、メソッドdoWork()
の最初に呼び出します。Find()
問題は、メソッドの継続時間とプログレス バーの長さを同期する方法がわからないことです。その上、それが私の目標を達成するための最良の方法であるかどうかはわかりません。
私のコード/状況に従って例を挙げてください。
ありがとう
編集: ここに私の目標があります: 私のメソッドは、JTextPane
( textpane.append()
) ユーザー ディレクトリからすべてのファイルを出力し、その後、各ファイル内の単語を検索します。JTextPane に印刷している間に JProgressBar を使用したいのですが、プロセス期間と同期する必要があります。可能な場合は、プリントの時間を計る必要があります:
例:
// 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