2016-12-11 10 views
0

私は、ドキュメントから文字列を読み込んで、指定された単語のすべての出現箇所を別の単語(ユーザ入力による)に置き換える作業をしています。Java JOptionPaneが作成されたスレッドのウィンドウからコンポーネントを表示しない

プログラムは、ファイルからバッファにデータを読み込むためのもの、文字列を変更するもの、出力を書き込むものの3つの別々のスレッドで実行されます。

ただし、チェックボックスがnotify-userとマークされている場合、特定の「ヒット」でサブストリングを置き換えるかどうかをユーザーに尋ねる必要があります。ここで問題は、JOptionPane.showConfirmDialog(...)を変更スレッドから使​​用しようとすると、ウィンドウには何もコンテンツ(空白のボックス)が含まれません。

私はSwingUtilities.InvokeLater(新しいRunnable(){... logic ...}を使って、確認ボックスを表示するようにしましたが、他のスレッドはpararellで実行し続けましたユーザー入力)を待つ。

/** 
* Checks the status of the string at each position in the buffer. If the status = Status.New and the String-object 
* matches to the string to replace then it will be replaced with the String-object replaceString. 
* <p> 
* If the Status of the object is anything other than Status.New then the thread will be blocked. 
* <p> 
* When done, the status of the modified object is changed to Status.Checked. 
*/ 
public synchronized void modify() { 
    try { 
     while (status[findPos] != Status.New) { 
      wait(); 
     } 

     String oldString = buffer[findPos]; 
     if (buffer[findPos].equals(findString)) { 
      buffer[findPos] = replace(findString, replaceString, start, findString.length()); 
     } 

     start += oldString.length() + 1; 
     status[findPos] = Status.Checked; 

     findPos = (findPos + 1) % maxSize; 

    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    notify(); 

} 

/** 
* Replaces the strSource with strReplace and marks the word in the source-tab JTextPane. The start argument 
* represents the index at position to replace the substring, the size argument represents the substring's 
* length. 
* 
* TODO : if notifyUser -> ask for user prompt before replacing. 
* 
* @param strSource : String 
* @param strReplace : String 
* @param start  : int 
* @param size  : int 
* @return s : String 
*/ 
public String replace(String strSource, String strReplace, int start, int size) { 
    String s = strSource; 
    DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
      new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); 


    //Ask user if he wants to replace the substring at position 'start'. 
    if (notifyUser) { 
     int x= JOptionPane.showConfirmDialog(null, "TEST", "TEST", JOptionPane.YES_NO_OPTION); 
    } else { 
     try { 

      textPaneSource.getHighlighter().addHighlight(start, start + size, 
        highlightPainter); 


     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 
     s = strReplace; 
     nbrReplacement++; 
    } 

    return s; 
} 
+0

を早いほど良いヘルプについては、投稿[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/) –

+0

あなたの質問は非常に幅広いようです;本質的には:differenで実行されるコードが必要な場合そのスレッドの振る舞いを「同期」させる。さて、あなたはそのためのコードを書く必要があります。言い換えれば、あなたの全体の設計/実装は、そのアイデアを踏まえて構築する必要があります。この権利を得るには何時間もの議論が必要です。私の提案:今はUI部品を忘れてしまいます。チュートリアルを見て、いくつかの作業でいくつかのスレッドを「同期」する方法を理解してください。そして、あなたがそれをマスターしたとき。あなたはUIのものを追加します。 – GhostCat

+0

ありがとう、しかし、同期は問題ではない、私は同時に実行して修正プログラムを作った。私はちょうど最初のifステートメントでJOPを表示させたい.replace(...)を呼び出して同期化された.modify()を呼び出すスレッドの1つからユーザー入力を取得できるようにしたい。 – 13120dde

答えて

0

を、私はあなたがスレッド間で共有されてThreadLocal変数が必要だと思う。そして、あなたがそれを中断したいすべてのスレッドでそれを確認する必要があります。

関連する問題