vtkPanel & native Render() issues when rearranging the swing components in a live JFrame (java, swing, vtk 9.2.6)

Hello Everyone,
I develop a Java swing application that has a user interface that allows moving the JComponents in a live JFrame.
I can totally instanciate my vtkPanels and add them to a JFrame I am building and then the jframe is working nicely when I call the .setVisible(true) on it.
However, the vtkPanel as some issues when I remove the vtkPanel from a live/shown JFrame and then reatach the vtkPanel in another place in the JFrame.
After I add the vtkPanel to another place in the JFrame, I often have an error writing this on the standart error stream:
2025-08-25 18:06:13.149 ( 117.401s) [ ]vtkWin32OpenGLRenderWin:267 ERR| vtkWin32OpenGLRenderWindow (000001271CF8C510): wglMakeCurrent failed in MakeCurrent(), error: 䧐᥄ħ
Usually lots of lines with this error, with just the timestamp modified.
Sometimes it seems the vtkPanel is still working properly after this, most often not (black vtkPanel), and sometimes it crashes the java app.

I gather the issue it that vtk doesn’t like to render its content while the vtkPanel is not visible and not attached to anything so I tried to override the Render() method to not call the vtkPanel.Render() original one when the vtkPanel is not added to anything, but it doesn’t seem to help that much.
I even tried to override addNotify() and removeNotify() to make Render() do nothing as long as addNotify() has not been called, but it does not work either.
Then just a few minutes ago I thought maybe the call to rw.setForceMakeCurrent() should be called only once for the component and so I modified it to test:

vtkPanel.java
 @Override
  public void addNotify()
  {
    super.addNotify();
    windowset = 0;
    rw.SetForceMakeCurrent();
    rendering = false;
  }
=> vtkRenderWindow.java
  private native void SetForceMakeCurrent_125();
  public void SetForceMakeCurrent()
  {
     if ( !setForceMakeCurrentDone ) {
        System.out.println("vtkRenderWindow.SetForceMakeCurrent()");
        setForceMakeCurrentDone= true;
        SetForceMakeCurrent_125();
     }
  }
  boolean setForceMakeCurrentDone;

but it did not help either.

In a nutshell, it seems the first call to Render() after the vtkPanel is reattached make it crash. I don’t know exactly why of what I can do to make it work properly.
Do you happen to know what I should do to ensute vtk is not lost when I rearrange the swing swing components in a live window?
Thanks in advance!

PS: by the way, I managed to find a workaround by… instantiating a new vtkPanel after I have finished to rearrange my components, but I don’t like this at all of course!

OK so, the c++ function with the error message is defined like this:

void vtkWin32OpenGLRenderWindow::MakeCurrent()
{
  // Try to avoid doing anything (for performance).
  HGLRC current = wglGetCurrentContext();
  if (this->ContextId != current)
  {
    if (wglMakeCurrent(this->DeviceContext, this->ContextId) != TRUE)
    {
      LPVOID lpMsgBuf;
      ::FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPWSTR)&lpMsgBuf, 0, nullptr);
      if (lpMsgBuf)
      {
        std::string message = vtksys::Encoding::ToNarrow((LPWSTR)&lpMsgBuf);
        vtkErrorMacro("wglMakeCurrent failed in MakeCurrent(), error: " << message.c_str());
        ::LocalFree(lpMsgBuf);
      }
    }
  }
}

The

HGLRC current = wglGetCurrentContext();
  if (this->ContextId != current)

shows that there is a context change, and I reckon it’s related to the swing components rearrangements (as shown in the addNotify() from vtkPanel that uses rw.SetForceMakeCurrent();to activate a flag that will force the call to makeCurrent, if I understand well).
And indeed if I call rw.SetForceMakeCurrent(); rw.MakeCurrent();
it fails after the MakeCurrent directly (not need to call a render)

… to be continued, I will be back :smiley:

Well, apparently the issues are (were?) caused by the various mouse & keyboard listeners in vtkPanel: they remain active when the detached & re attached vtkPanel is not totally ready, and so some issues appear in the native code at this moment.
I think the wglMakeCurrent issues are because of that.