【发布时间】:2014-10-21 20:29:41
【问题描述】:
我正在尝试使用 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 方法和/或 process 方法中更新 textarea 值。
【问题讨论】:
-
这段代码太长且难以理解,尤其是因为您的许多变量不是英文的。请通过How to Ask查看帮助中心
-
在运行工作之前从字段中获取文本,否则您可能会遇到竞争条件
-
我提高了代码的可读性
标签: java swing jtextarea swingworker