I'm trying use the SwinWorker to update one TextArea with the status of the execution of one button actionPerform method.
I get various samples from internet and nothing work form me. Anybody can help me?
bellow my button method:
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 is the JtextArea that I'm trying to update.
refreshTextArea(String, boolean) is the method that I have created to update the 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();
}
In this case I have created all process in the method scope, but I was tried using a separated class, as bellow:
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);
}
}
}
In both implementations I have tried using with and without execution of revalidate method of textarea. In both implementations I have tried update the textarea value in doBackground method and/or process method.