У меня есть MainPanel, в котором у меня есть SwingWorker с doInBackgroung, в котором я вызываю службу, которая вызывает DAO, который выполняет запрос данных из базы данных.
У меня также есть кнопка остановки, при нажатии которой устанавливается worker.cancel(true), но программа не останавливается, и я даже не могу закрыть окно с помощью [X] Кнопка.
Я предполагаю, что обработка не прекращается немедленно, поскольку запрос к базе данных еще не выполнен, но почему я не могу закрыть окно?
Это код:
Код: Выделить всё
worker = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
long startTime = System.nanoTime();
textArea.append("Starting...\n");
generatingFilesService.genereteFiles(connectionDBFirst, connectionDBSecond, connectionDBThird,
date1, date2);
long endTime = System.nanoTime();
double time = (double) ((endTime - startTime) / 1_000_000_000);
if (ConnectionDBFirst.flag != false) {
if (time < 60d) {
textArea.append("Genereting ended for " + time + " seconds\n");
textArea.setCaretPosition(MainPanel.textArea.getDocument().getLength());
} else {
textArea.append("Genereting ended for " + (time / 60) + " minutes\n");
textArea.setCaretPosition(MainPanel.textArea.getDocument().getLength());
}
}
return null;
}
@Override
protected void done() {
if (isCancelled()) {
textArea.append("Stopping generating files...\n");
closeConnections();
logger.info(Messages.PROCESS_INTERUPTED);
} else
closeConnections();
}
};worker.execute();
Код: Выделить всё
if (e.getSource() == stop) {
worker.cancel(true);
stop.setEnabled(false);
}
На основании ответа и комментариев, вот остальная часть кода:
Код: Выделить всё
public void genereteFiles(connectionDBFirst, connectionDBSecond, connectionDBThird,
date1, date2) {
List
persons1 = daoFirst.getPersons(connectionDBFirst, date1, date2);
// Here I want to update the textArea() that the daoFirst.getPersons() is done
// but how to that without MainPanel.textArea.append(...);
List persons2 = daoSecond.getPersons(connectionDBSecond, date1, date2);
// Here I want to update the textArea() that the daoSecond.getPersons() is done
// but how to that without MainPanel.textArea.append(...);
// same with daoThird
genereteFilesService.createFiles(persons1, persons2, persons3);
// update textArea() again
}
Код: Выделить всё
public List
getPersons(connectionDBFirst, date1, date2) {
// Executing SQL query and getting the persons
// This takes long time, and from here I calculate
// percentage of persons fetched and I want to show the percentage in textArea()
// but how to do that without MainPanel.textArea.append(...);
}
>
Подробнее здесь: https://stackoverflow.com/questions/760 ... update-gui
Мобильная версия