gpt4 book ai didi

java - 捕获位于不同类中的 SwingWorker 抛出的异常

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:49:45 24 4
gpt4 key购买 nike

我有一个主类 Gui 来处理我的 GUI 并调用几个 SwingWorker(调用 DB 和其他数据源),这些 SwingWorker 已放在它们自己的类 TablesDataManager 中.我的问题是,当其中一个 swingworker 在其 done90 方法中(即在 EDT 上)抛出异常时,我希望能够在主类中以某种方式捕获它并采取相应的行动(在中调用 showErrorAndExit下面是我的 SSCCE)。

欢迎提出任何想法。

图形界面.java

import java.awt.event.WindowEvent;
import javax.swing.*;

public class Gui extends JFrame {

private final JLabel waitLabel = new JLabel();
private final JPanel panel = new JPanel();
private final TablesDataManager tblData = new TablesDataManager();

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
createAndShowGui();
}
});
}

private static void createAndShowGui() {
Gui frame = new Gui("My Great GUI");
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public Gui(String title) {
super(title);
panel.add(waitLabel);
setContentPane(panel);
try {
initData();
} catch (Exception e) {
showErrorAndExit(e);
}
}

//I WANT TO CALL THIS METHOD IF done() THROWS AN EXCEPTION
private void showErrorAndExit(Exception e) {
JOptionPane.showMessageDialog(this,
"An unexpected error occured while initialising the tables. The application will close.\n"
+ (e.getMessage() == null ? "" : e.getMessage()),
"Unrecoverable error",
JOptionPane.ERROR_MESSAGE);
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}

private void initData() {
waitLabel.setText("Loading Data");
tblData.initData(new Runnable() {

public void run() {
initTables();
}
});
}

private void initTables() {
waitLabel.setText("Loading Tables");
tblData.initTables(new Runnable() {

public void run() {
finishComponentsSetup();
}
});
}

private void finishComponentsSetup() {
waitLabel.setText("We are done");
}
}

TablesDataManager.java

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;

class TablesDataManager {

private final InitData initData = new InitData();
private final InitTables initTables = new InitTables();

void initData(final Runnable runAfterInit) {
launchWorker(initData, runAfterInit);
}

void initTables(final Runnable runAfterInit) {
launchWorker(initTables, runAfterInit);
}

private void launchWorker(final SimpleSwingWorker worker, final Runnable runAfterWorkerDone) {
worker.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("progress")) {
if (worker.getProgress() == 100) { //update finished
runAfterWorkerDone.run();
}
}
}
});
worker.execute();
}

private class InitData extends SimpleSwingWorker {

@Override
protected Void doInBackground() throws Exception {
//do something in the background
//unfortunately there is a connection problem
throw new IOException("Can't connect to database...");
}
}

private class InitTables extends SimpleSwingWorker {

@Override
protected Void doInBackground() throws Exception {
//do something else in the background
return null;
}
}


private abstract class SimpleSwingWorker extends SwingWorker<Void, Void> {

@Override
protected abstract Void doInBackground() throws Exception;

@Override
public void done() {
try {
get();
setProgress(100);
} catch (ExecutionException | InterruptedException e) {
System.out.println("Got that?");
//WHAT DO I DO WITH IT???
throw new RuntimeException(e);
}
}
}
}

最佳答案

在您的 GUI 中保留对您的工作人员的引用,并在您的工作中添加一个 try catch。在 catch 中,将异常分配给一个变量并向其添加一个 getter。在您的 GUI 中,当您的 worker 完成后,只需检查 worker 中是否存在异常。

关于java - 捕获位于不同类中的 SwingWorker 抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9502149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com