我正在研究一个应用程序,该应用程序从文档中读取字符串,并用另一个单词替换给定单词的每一次出现(通过用户输入)。
该程序使用三个单独的线程运行,一个用于从文件到缓冲区的数据,一个用于修改字符串,一个用于编写输出。
但是,如果将复选框标记为Notify-user,则我需要询问用户是否要在给定的" hit"上替换子字符串。现在是问题,当我尝试使用joptionpane.showconfirmdialog(...)中时,窗口不包含任何内容(空白的白色框)。
我还尝试使用swingutilities.invokelater(new 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;
}
我认为您需要一个线程之间共享的线程列表变量。然后,您必须在要暂停的每个线程中检查它。