하나의 버튼 actionPerform 메소드의 실행 상태로 하나의 TextArea를 업데이트하려면 SwinWorker를 사용하려고합니다.SwingWorker로 JTextArea 업데이트
인터넷에서 다양한 샘플을 얻을 수 있으며 작업 양식이 없습니다. 아무도 나 도와 줄 수 없어? 내 버튼 방식 우는 소리
:
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 내가 JTextArea에
I 메소드 범위의 모든 프로세스를 작성한 이때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();
}
를 업데이트 생성 한 방법이지만, I는 분리 된 클래스를 사용하려고하여, 울부 짖음 :
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] (http://stackoverflow.com/help/how-to-ask)에서 도움말 센터를 확인하십시오. – durron597
작업을 실행하기 전에 필드의 텍스트를 가져 오면, 그렇지 않으면 경쟁 조건이 발생할 수 있습니다 – MadProgrammer
코드 가독성이 향상되었습니다 – user1352652