Using newline in JFace ErrorDialog

橙三吉。 提交于 2019-12-13 06:34:32

问题


Has anybody managed to show a detail error message in the JFace ErrorDialog using newline (\n, \r) character?

I'm trying to display the StackTrace as a detail message, but it is only shown in one line

StringBuffer sbStackTrace = new StringBuffer();
for (StackTraceElement ste : throwable.getStackTrace()) {
  sbStackTrace.append(ste.toString()).append('\n');
}

final IStatus status = new Status(IStatus.ERROR, pluginId, statusMsg, new Throwable(sbStackTrace.toString(), throwable));
ErrorDialog dialog = new ErrorDialog(shell, "Error", customMessage, status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR)

On open the ErrorDialog shows eveything I want, except that the detail message is not properly formatted.

Any ideas?


回答1:


Looking at the ErrorDialog.populateList(List, IStatus, int, boolean) method in the source for ErrorDialog.java, it looks like the dialog only includes one IStatus per line, but if that IStatus has children, it will include them on subsequent lines. So you'll need to either build a MultiStatus object or create your own implementation of IStatus.




回答2:


I seem to remember this working with just '\n' but I was also using:

ExceptionUtils.getStackTrace( ex );

From Apache Commons-Lang.




回答3:


To achieve the same it is also possible to build MultiStatus. MultiStatus can have next IStatus(es) as children and because ErrorDialog in detail area is showing one IStatus per line, you will get the same result.

Please see the trick in action in my other reply https://stackoverflow.com/a/9404081/915931




回答4:


Wrap the Exception with a new that contains the stacktrace as message.

The code is here https://stackoverflow.com/a/19703255/2064294



来源:https://stackoverflow.com/questions/4694187/using-newline-in-jface-errordialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!