2012-05-06 17 views
1

textAreaと呼ばれるこのJTextAreaを更新しようとしていますが、これらの写真をコピーしている間に更新しても動作するようには思えません。JTextAreaのリアルタイム更新が必要です

String name = ""; 
    int numberOfPicturesCopied = 0; 
    while (pictures.isEmpty() == f) { 
     try { 
      File tmp = pictures.firstElement(); 
      name = tmp.getName(); 
      String filename = destination + Meta.date(tmp) + tmp.getName(); 
      Path source = tmp.toPath(); 
      File destFile = new File(filename); 
      Path destination = destFile.toPath(); 
      Files.copy(source, destination, 
        StandardCopyOption.COPY_ATTRIBUTES); 
      textArea.append("Copied " + name + "\n"); 
      pictures.removeElementAt(0); 
      numberOfPicturesCopied++; 
     } catch (FileAlreadyExistsException faee) { 
      textArea.append("Skipped " + name 
        + ": Picture Already In Computer\n"); 
     } catch (NoSuchFileException ncfe) { 
      File tmp = pictures.firstElement(); 
      String filename = destination + Meta.date(tmp); 
      File newDir = new File(filename); 
      newDir.mkdir(); 
     } catch (IOException ee) { 
      // TODO Auto-generated catch block 
      ee.printStackTrace(); 
     } 
    } 

をして、私はこれにそれを変更:私はこのコードを使用していたのと同じ結果に

public void copyPictures(){ 
    SwingUtilities.invokeLater(new Thread(){ 
     public void run(){ 
      String name = ""; 
      while(pictures.isEmpty() == f){ 
       try { 
        File tmp = pictures.firstElement(); 
        name = tmp.getName(); 
        String filename = destination + Meta.date(tmp) + tmp.getName(); 
        Path source = tmp.toPath(); 
        File destFile = new File(filename); 
        Path destination = destFile.toPath(); 
        Files.copy(source, destination, StandardCopyOption.COPY_ATTRIBUTES); 
        textArea.append("Copied " + name + "\n"); 
        pictures.removeElementAt(0); 
        numberOfPicturesCopied++; 
       } catch(FileAlreadyExistsException faee){ 
        textArea.append("Skipped " + name +": Picture Already In Computer\n"); 
       } catch (NoSuchFileException ncfe){ 
        File tmp = pictures.firstElement(); 
        String filename = destination + Meta.date(tmp); 
        File newDir = new File(filename); 
        newDir.mkdir(); 
       } catch (IOException ee) { 
        // TODO Auto-generated catch block 
        ee.printStackTrace(); 
       } 
      } 
     } 
    }); 
} 

。助言がありますか?

また、テキスト領域の上部にテキストを入力する方法はありますか?あなたが求めているもの

+0

1)すぐに役立つようにするには、[SSCCE]( http://sscce.org/)を試してみてください。試したことの他のものは、(例えば) 'copyPictures2()'メソッドのように指定することができます。 2)あなたがEDTをブロックしているようです。 'SwingWorker'を使います。 –

+0

'SwingUtilities.invokeLater(new Thread())'が間違っています。バックグラウンドスレッドで実行したいファイルI/O作業を、EDTで実行するSwingコンポーネントの更新から完全に分離する必要があります。 – wolfcastle

答えて

0

わからない、タイトルは、その場合は

...テキストありえない更新は、しかし、あなたの質問は、あなたがそれになりたい場所それが挿入されるイマイチであることを示しているようだと言っているように見えます後者の場合は、代わりに挿入メソッドを使用してください。

textArea.insert("Copied " + name + "\n",0); 

テキスト領域の最上部に挿入します。

1

最初にテキストを挿入する方法は既に回答済みです。あなたの質問の他の部分はいつもと同じです...イベントディスパッチスレッドで重い作業をしていますが、これはもはや再ペイントを実行できません。

何がすべきかは、ワーカースレッドで大量の作業を行い、EDTのUIのみを更新することです。たとえば、これのために設計されたSwingWorkerを使用できます。あるいはさらに簡単、あなたの現在のコードを取ると、いくつかの簡単な修正を加えた

public void copyPictures(){ 
    new Thread(){ 
     public void run(){ 
      while(pictures.isEmpty() == f){ 
       try { 
        File tmp = pictures.firstElement(); 
        final String name = tmp.getName(); 
        String filename = destination + Meta.date(tmp) + tmp.getName(); 
        Path source = tmp.toPath(); 
        File destFile = new File(filename); 
        Path destination = destFile.toPath(); 
        Files.copy(source, destination, StandardCopyOption.COPY_ATTRIBUTES); 

        SwingUtilities.invokeLater( 
         new Runnable(){ 
         public void run(){ 
         textArea.append("Copied " + name + "\n"); 
         } 
         } 
        );      

        pictures.removeElementAt(0); 
        numberOfPicturesCopied++; 
       } catch(FileAlreadyExistsException faee){ 
        textArea.append("Skipped " + name +": Picture Already In Computer\n"); 
       } catch (NoSuchFileException ncfe){ 
        File tmp = pictures.firstElement(); 
        String filename = destination + Meta.date(tmp); 
        File newDir = new File(filename); 
        newDir.mkdir(); 
       } catch (IOException ee) { 
        // TODO Auto-generated catch block 
        ee.printStackTrace(); 
       } 
      } 
     } 
    }.run(); 
} 

作業がまだUIがEDTに更新された別のThreadで行われている方法を参照してください。詳細はSwing Concurrency tutorialまたはSOにあります(キーワードはです)。

関連する問題