[英]JTextArea not updating in real time despite attempt at correct threading
這是“我如何獲得一個JTextArea實時更新”問題中的另一個問題。 我已經閱讀了現有的每一篇帖子,並且我認為自己對此進行了正確的處理。 但是,我仍然遇到同樣的問題-我的JTextArea直到循環完成后才更新,然后一次刷新就更新了。 有人可以看到我在哪里做錯了嗎? 我看不到 謝謝!
編輯:切換文本區域更新調用的方法。 但是我仍然有相同的結果!
private void saveImagesToDisk() {
textArea.append("\nPreparing to save photos to photos directory\n\n");
for (MediaFeedData mfd : imagesMetaDataList) {
try {
String imageURL = mfd.getImages().getLowResolution().getImageUrl();
final String filename = mfd.getId(); // just name the file using the image id from instagram
System.out.println(filename);
SaveImageFromUrl.saveImage(imageURL, filename, textArea);
} catch (IOException e) {
e.printStackTrace();
}
}
}
然后,在save方法中,我有以下內容:
public class SaveImageFromUrl {
public static Boolean saveImage(final String imageUrl, String destinationFile, final JTextArea textArea) throws IOException {
String directoryName = "photos";
if (!makePhotosDirectory(directoryName, textArea)) {return false;}
File file = new File(directoryName, destinationFile);
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
new Thread() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(imageUrl + " written to photos directory\n");
}
});
}
}.start();
return true;
}
for循環和下面方法中的代碼都需要在后台線程中調用,而您無需這樣做。
if (SaveImageFromUrl.saveImage(imageURL, filename, textArea)) {
如果耗時的代碼不是在后台調用的代碼,僅使用后台線程將無濟於事。
考慮使用SwingWorker,類似...
private void saveImagesToDisk() {
textArea.append("\nPreparing to save photos to photos directory\n\n");
final SwingWorker<Void, String> imageWorker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
for (MediaFeedData mfd : imagesMetaDataList) {
final String imageURL = mfd.getImages().getLowResolution().getImageUrl();
final String filename = mfd.getId();
System.out.println(filename);
String textToPublish = "";
if (SaveImageFromUrl.saveImage(imageURL, filename, textArea)) {
textToPublish = filename + " written to photos directory\n";
} else {
textToPublish = filename + " not saved!\n";
}
// publish String so it can be used in the process method
publish(textToPublish);
}
return null;
}
@Override
protected void process(List<String> chunks) {
// Strings sent to the EDT by the publish method
// This called on the Swing event thread.
for (String chunk : chunks) {
textArea.append(chunk);
}
}
};
imageWorker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
try {
// need to call this on the event thread
// to catch any exceptions that have occurred int he Swing Worker
imageWorker.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
// TODO catch exceptions buried in this guy
e.printStackTrace();
}
}
}
});
// run our SwingWorker
imageWorker.execute();
}
有關更多詳細信息,請閱讀Swing中的並發 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.