我尝试使用SwinWorker更新一个TextArea,其状态为执行一个按钮actionPerform方法。
我从互联网上获得了各种样本,没有任何形式来自我。有人可以帮帮我吗?
按照我的按钮方法:
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
atualizaTextArea("", true);
button.setEnabled(false);
SearchDataBase cd = new SearchDataBase();
textAreaField.setForeground(Color.BLUE);
try {
refreshTextArea("......Process Started......\n\n", true);
cd.search();
String textt = textAreaField.getText();
refreshTextArea("Finish\n\n", false);
} catch (Exception e) {
button.setEnabled(true);
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
textAreaField.setText("Error - Contact the developer");
textAreaField.setForeground(Color.red);
}
if (comboBoxSearchType.getSelectedIndex() == 1) {
try {
refreshTextArea("......History search started (too slow!!)......\n\n", false);
cd.searchHistoryTable(dateChoserPesquisa.getDate().getTime());
refreshTextArea("Finish\n\n", false);
} catch (Exception e) {
button.setEnabled(true);
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
textAreaField.setText("Error - Contact the developer");
textAreaField.setForeground(Color.red);
}
}
refreshTextArea("...............Finish..............", false);
button.setEnabled(true);
}
TextAreaAviso是我尝试更新的JtextArea。
refreshTextArea(String,boolean)是我创建的用于更新JTextArea的方法
private void refreshTextArea(String text, boolean rep) {
this.texttt = text;
this.replace = rep;
// define a SwingWorker to run in background
SwingWorker<String, String> worker = new SwingWorker<String, String>() {
@Override
public String doInBackground() {
String t = textAreaField.getText();
if (replace) {
publish(texttt);
t = texttt;
} else {
publish(t + texttt);
t+= texttt;
}
System.out.println(texttt);
return "";
}
@Override
protected void process(final List<String> chunks) {
for (String text : chunks) {
TextAreaAviso.setText(texttt);
System.out.println("HERE!!!!");
}
}
};
worker.execute();
// (new AtualizaTextArea(TextAreaAviso, texto, replace)).execute();
}
在这种情况下,我已经在方法范围内创建了所有进程,但我尝试使用一个单独的类,如下所示:
private void refreshTextArea(String text, boolean rep) {
(new RefreshTextArea(TextAreaAviso, texto, replace)).execute();
}
class RefreshTextArea extends SwingWorker<Integer, String> {
private JTextArea textArea;
private String texto;
private boolean replace;
public RefreshTextArea(JTextArea textArea, String texto, boolean replace) {
this.textArea = textArea;
this.texttt = texto;
this.replace = replace;
}
@Override
protected Integer doInBackground() throws Exception {
int i = 0;
String t = "";
if (!replace) {
t = texttt + textArea.getText();
} else {
t = texttt;
}
System.out.println(t);
publish(t);
return i;
}
@Override
protected void process(final List<String> chunks) {
for (String text : chunks) {
textArea.setText(texttt);
}
}
}
在两个实现中,我尝试使用和不执行textarea的revalidate方法。 在这两个实现中,我都尝试在doBackground方法和/或进程方法中更新textarea值。
答案 0 :(得分:3)
您的SwingWorker
JTextArea
中间结果并更新doInBackground()
中的publish()
。此外,JTextArea
将具有取代现有文本的效果;您可能需要process()
。显示了一个完整的示例here。请注意,setText()
可以访问封闭范围中的append()
,但您也可以将其作为参数传递给BackgroundTask
。