スウィングワーカーを使用してファイルを検証する startLineとendLineを検証クラスに送信して、毎回ファイル全体を検証しないようにします。初めて既存のファイルがopenendの場合、startLineを0、endLineをendLine = editorTextArea.getLineCount() - 1;として送信します。その後、startLineとendLineを毎秒私の便宜のために送信できるはずです。どのように私はこれを達成するのですか?スウィングワーカーを異なるパラメータで別の場所に呼び出す
検証クラス:
class Validate implements Runnable {
private JTextArea editorTextArea;
private JTextArea errorTextArea;
private int startLine;
private int endLine;
public Validate(JTextArea editor, JTextArea error, startLine, endLine) {
this.editorTextArea = editor;
this.errorTextArea = error;
this.startLine = startLine;
this.endLine = endLine;
}
@Override
public void run() {
SwingWorker worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
//CODE TO VALIDATE
return null;
}
@Override
protected void done() {
//CODE TO DISPLAY THE RESULT
}
};
worker.execute();
}
}
メインクラス:
//Calls the validate
public void taskExecutor() {
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
final ScheduledFuture<?> timeHandle =
scheduler.scheduleAtFixedRate(new Validate(editorTextArea, errorTextArea), 0, 1, SECONDS);
}
private void openFileActionPerformed(java.awt.event.ActionEvent evt) {
fileChooser.setCurrentDirectory(new File(".txt"));
int result = fileChooser.showOpenDialog(new JPanel());
int totLines = 0;
String[] content = null;
if (result == JFileChooser.APPROVE_OPTION) {
try {
filename = String.valueOf(fileChooser.getSelectedFile());
setTitle(filename);
FileReader fr = new FileReader(filename);
editorTextArea.read(fr, null);
fr.close();
startLine = 0;
endLine = editorTextArea.getLineCount() - 1;
//here i want to call the validate class once. that is askExecutor();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
ありがとうございました。私は実行可能ファイルを削除しました。シナリオは、ファイルが開かれたときにファイルを完全に検証する必要があります。行番号、0とtotallinesを送信して実行できます。しかし、次回以降、ファイルがjtextareaで修正/訂正されたとき、バリデーションはビューポートに表示されるパーツに対してのみ行われます。どのようなイベントで私はswingworkerを検証する必要がありますか、私はdocumentListenerのinsertUpdateメソッドに配置する必要がありますか? – FirmView
@FirmView:ああ、私は誤解しました。ワーカのコンストラクタにパラメータとしてライン番号を渡すと考えられます。以上。 – trashgod
私はあなたの努力のためにupvotingです。私はスイングワーカーと呼ぶべきイベントが何であるかを聞いていました。初めてファイルを開くときに、ドキュメントリスナーメソッドと呼ぶ必要がありますか? – FirmView