私はここで私の問題に対する答えを見つけようとしましたが、その豊富さと多様性のためにいくらか混乱しました。ここで私の質問です:私のアプリは2つのファイルを比較し、Swing.JTextPane
の結果を出力します。私はボタンでファイルを処理するコードを呼び出し、私はSwingWorker
でファイルの各ペアを処理するUIを停止することを避けるために。ここではそのコードは次のとおりです。doInBackground()
比較を行いSwingWorkerが終了するのを待ってからもう一度実行してください
class ProcessAndPrintTask extends SwingWorker<Void, Void> {
private Report report;
Integer reportResult;
ProcessAndPrintTask(Report report) {
this.report = report;
reportResult = null;
}
@Override
protected Void doInBackground() {
try {
reportResult = report.getComparator().compareTwoFiles(new FileInputStream(new File(pathToReportsA + report.getFilename())),
new FileInputStream(new File(pathToReportsB + report.getFilename())));
}
catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void done() {
String message = report.getFilename() + ": ";
if (reportResult != null) {
switch (reportResult) {
case 1:
StyleConstants.setBackground(style, Color.GREEN);
try {
doc.insertString(doc.getLength(), message + "MATCH\n", style);
}
catch (BadLocationException ex) {ex.printStackTrace();}
break;
case 0:
StyleConstants.setBackground(style, Color.RED);
try {
doc.insertString(doc.getLength(), message + "NO MATCH\n\n", style);
try {
for (String s : report.getComparator().getDifferences(
new FileInputStream(new File(pathToReportsA + report.getFilename())),
new FileInputStream(new File(pathToReportsB + report.getFilename())))) {
doc.insertString(doc.getLength(), s + "\n", style);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
catch (BadLocationException ex) {ex.printStackTrace();}
break;
case -1:
StyleConstants.setBackground(style, Color.CYAN);
try {
doc.insertString(doc.getLength(), message + "BOTH FILES EMPTY\n", style);
}
catch (BadLocationException ex) {ex.printStackTrace();}
break;
default:
StyleConstants.setBackground(style, Color.ORANGE);
try {
doc.insertString(doc.getLength(), message + "PROBLEM\n", style);
}
catch (BadLocationException ex) {ex.printStackTrace();}
}
}
else {
StyleConstants.setBackground(style, Color.ORANGE);
try {
doc.insertString(doc.getLength(), message + "FILE OR FILES NOT FOUND\n", style);
}
catch (BadLocationException ex) {ex.printStackTrace();}
}
}
}
、done()
は、比較の結果に応じてメッセージをフォーマットし、それを印刷します。問題は、プログラムが1ペアが処理されて印刷されるまで待たずに、結果が開かれた順番に印刷されないようにすることです。これはユーザーにとって非常に混乱する可能性があります。ファイルのほとんどは小さく、したがって、比較はある時点で完了したように見えますが、まだ処理されている大きなファイルがあります。
私はPropertyChangeListener
を使用しての可能性について読んで私はそれがdone()
メソッドを使用してからどのように異なるかを見ていない...ことである(私はdoInBackground()
の両方を比較して印刷を実行しようとしたが、これは書式設定を台無しに印刷が完了する前に、背景色が変更されます)。醜い松葉杖のようです
try (FileInputStream reportListExcelFile = new FileInputStream(new File(reportListPath))) {
Workbook workbook = new XSSFWorkbook(reportListExcelFile);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> iter = sheet.iterator();
// skip first row that contains columns names
iter.next();
while (iter.hasNext()) {
try {Thread.sleep(1000);} catch (Exception ex) {ex.printStackTrace();}
Row r = iter.next();
String name = r.getCell(0).getStringCellValue();
String format = r.getCell(1).getStringCellValue();
Report currentReport = new Report(name, format);
new ProcessAndPrintTask(currentReport).execute();
}
}
だけでなく、GUIは、すべてのファイルまでハングアップする原因:私はまた、このように見えたSwingWorker
を呼び出すループ内の任意の時間のためにThread.sleep()
を起動しようとしましたペアを比較した。
解決策はありますか?
あなたは単に起動する必要があります最初の 'done'メソッドからの2番目の' SwingWorker'また 'SwingWorker'を1つだけ使用して、両方のタスクを1つの' doInBackground'メソッドで開始することができます。 –
しかし、私は 'SwingWorker'に' Report'のコレクションを渡さなければなりませんでした。 – DCzo
それを試して、完璧に働いた - ありがとう! – DCzo