错误代码
// 使用 ProgressManager 显示进度条
ProgressManager progressManager = ProgressManager.getInstance();
progressManager.run(new Task.Backgroundable(ProjectUtils.getCurrProject(), "Saving Data Source Configurations") {
@Override
public void run(@NotNull ProgressIndicator indicator) {
// 在这里执行保存操作
instance.mergeAndClearConnectionInfos(databaseConnectionInfos);
JTree databaseTree = null;
try {
databaseTree = DataBaseToolWindowContent.getInstants().getDatabaseTree();
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
ConnectionInfoTreeUtil.refreshTree(databaseTree);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
正确代码
// 使用 ProgressManager 显示进度条
ProgressManager progressManager = ProgressManager.getInstance();
progressManager.run(new Task.Backgroundable(ProjectUtils.getCurrProject(), "Saving Data Source Configurations") {
@Override
public void run(@NotNull ProgressIndicator indicator) {
// 在这里执行保存操作
instance.mergeAndClearConnectionInfos(databaseConnectionInfos);
JTree databaseTree = null;
try {
databaseTree = DataBaseToolWindowContent.getInstants().getDatabaseTree();
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
ConnectionInfoTreeUtil.refreshTree(databaseTree);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
线程问题: 在非沙箱环境中,确保在 UI 操作中使用正确的线程。刷新 UI 的操作通常应该在 UI 线程上执行。可以使用 ApplicationManager.getApplication().invokeLater() 来确保在正确的线程上执行刷新操作。