how to add a JBCefBrowser in toolWindow?

Answered

hi , 

I want to show a webview in toolWindow.

im start with this tow documents.

https://plugins.jetbrains.com/docs/intellij/jcef.html?from=jetbrains.org#enabling-jcef

https://github.com/JetBrains/intellij-sdk-docs/tree/main/code_samples/tool_window/src/main/java/org/intellij/sdk/toolWindow

 

1. registe toolwindow in plugin.xml

<extensions defaultExtensionNs="com.intellij">
<toolWindow id="MyPanel" anchor="right" factoryClass="plugindemo.MyPalelFactory"/>
<!-- Add your extensions here -->
</extensions>

2. create a  swing form.

public class MyPanel {
private JPanel webviewPanel;
public MyPanel(ToolWindow toolWindow){
webviewPanel.add(new JBCefBrowser("http://xxx.com").getComponent());
}
public JPanel getContent() {
return webviewPanel;
}
}

3. add Factory 

public class MyPalelFactory implements ToolWindowFactory {

public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
MyPanel myToolWindow = new MyPanel(toolWindow);
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(myToolWindow.getContent(), "", false);
toolWindow.getContentManager().addContent(content);
}

}

when i run intellij:runIde then i got a java.lang.NullPointerException and webview can't shown in toolwindow. 

why does this happened and how to fix it?

2021-07-13 17:05:39,420 [  11555]  ERROR - .wm.impl.ToolWindowManagerImpl - Cannot init toolwindow plugindemo.MyPanelFactory@4955eb5f 
java.lang.NullPointerException
at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:134)
at java.desktop/java.awt.Container.addImpl(Container.java:1152)
at java.desktop/java.awt.Container.add(Container.java:436)
at plugindemo.MyPanel.<init>(MyPanel.java:17)
at plugindemo.MyPanelFactory.createToolWindowContent(MyPanelFactory.java:13)
at com.intellij.openapi.wm.impl.ToolWindowImpl.createContentIfNeeded$intellij_platform_ide_impl(ToolWindowImpl.kt:499)
at com.intellij.openapi.wm.impl.ToolWindowImpl.scheduleContentInitializationIfNeeded$intellij_platform_ide_impl(ToolWindowImpl.kt:479)
at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.doShowWindow(ToolWindowManagerImpl.kt:966)
at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.showToolWindowImpl(ToolWindowManagerImpl.kt:912)
at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.doRegisterToolWindow(ToolWindowManagerImpl.kt:1036)
at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.initToolWindows(ToolWindowManagerImpl.kt:487)
at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.access$initToolWindows(ToolWindowManagerImpl.kt:92)
at com.intellij.openapi.wm.impl.ToolWindowManagerImpl$beforeProjectOpened$2.run(ToolWindowManagerImpl.kt:423)
at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:201)
at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:802)
at com.intellij.openapi.application.impl.ApplicationImpl.lambda$invokeLater$4(ApplicationImpl.java:322)
at com.intellij.openapi.application.impl.FlushQueue.doRun(FlushQueue.java:84)
at com.intellij.openapi.application.impl.FlushQueue.runNextEvent(FlushQueue.java:132)
at com.intellij.openapi.application.impl.FlushQueue.flushNow(FlushQueue.java:47)
at com.intellij.openapi.application.impl.FlushQueue$FlushNow.run(FlushQueue.java:188)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:776)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:746)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:971)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:841)
at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:452)
at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:744)
at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$9(IdeEventQueue.java:451)
at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:802)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:499)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
1 comment
Comment actions Permalink

private
JPanel webviewPanel;

is never initialized, thus throwing NullPointerException

0

Please sign in to leave a comment.