"Acces is allowed from event dispatch thread only" When Creating Content for Tool Window

Answered

I am building a plugin where one of the Actions runs a process and displays the output in a custom tool window. I need this to be done in a separate thread because the process takes multiple seconds and if it is run from the event dispatch thread then it freezes the whole IntelliJ GUI. Below is the full error:

Access is allowed from event dispatch thread only.

com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@34e4c4e4
Current thread: Thread[Thread-5,6,Idea Thread Group] 2027524414
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,Idea Thread Group] 291440039
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1128)
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1117)
at com.intellij.openapi.editor.impl.EditorImpl.assertIsDispatchThread(EditorImpl.java:3266)
at com.intellij.openapi.editor.impl.EditorImpl.<init>(EditorImpl.java:348)
at com.intellij.openapi.editor.impl.EditorFactoryImpl.createEditor(EditorFactoryImpl.java:201)
at com.intellij.openapi.editor.impl.EditorFactoryImpl.createViewer(EditorFactoryImpl.java:171)
at com.intellij.execution.impl.ConsoleViewUtil.setupConsoleEditor(ConsoleViewUtil.java:63)
at com.intellij.execution.impl.ConsoleViewImpl.doCreateConsoleEditor(ConsoleViewImpl.java:951)
at com.intellij.execution.impl.ConsoleViewImpl.lambda$createConsoleEditor$12(ConsoleViewImpl.java:929)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:974)
at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:57)
at com.intellij.execution.impl.ConsoleViewImpl.createConsoleEditor(ConsoleViewImpl.java:928)
at com.intellij.execution.impl.ConsoleViewImpl.initConsoleEditor(ConsoleViewImpl.java:431)
at com.intellij.execution.impl.ConsoleViewImpl.getComponent(ConsoleViewImpl.java:410)
at com.insidesecure.plugin.structure.ToolingAPI.executeTask(ToolingAPI.java:42)
at com.insidesecure.plugin.action.BuildAnalysisGTA$1.run(BuildAnalysisGTA.java:39)
at java.lang.Thread.run(Thread.java:748)

The line in the method ToolingAPI.executeTask (42) that causes the error is this:

Content content = cpWindow.getContentManager().getFactory().createContent(consoleView.getComponent(), task, false);

How can I create a tab in a tool window and continuously print to it whenever I need to from within a thread that is called from the main event dispatch thread?

Thank you.

0
2 comments

Tool window could be initialized using com.intellij.openapi.wm.ToolWindowFactory instead. See http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html as reference

1

Thank you again Yann! That was a great read, wish i had come across it sooner.

0

Please sign in to leave a comment.