I apologize for the length of this example (it&#39;s a little long), but it is totally self contained and shows my the error that I&#39;m having.  Please let me know if you need any more information, or the tweaks to make what I have work.<div>
<br></div><div><div>import vtk.*;</div><div>import javax.swing.*;</div><div>import java.awt.event.*;</div><div>import java.awt.*;</div><div><br></div><div>public class VTKSwingError {</div><div>    static {</div><div>        System.loadLibrary(&quot;vtkCommonJava&quot;);</div>
<div>        System.loadLibrary(&quot;vtkFilteringJava&quot;);</div><div>    }</div><div><br></div><div>    static JPanel topPanel;</div><div>    static JPanel bottomPanel;</div><div>    static JFrame frame;</div><div><br>
</div><div>    // This is just creating two different images so we can tell when they&#39;re swapped</div><div>    public static JPanel createImagePanel(boolean useMod0) {</div><div>        vtkImageData image = new vtkImageData();</div>
<div>        image.SetSpacing(1, 1, 1);</div><div>        image.SetOrigin(0, 0, 0);</div><div>        image.SetExtent(0, 511, 0, 511, 0, 0);</div><div>        image.AllocateScalars();</div><div>        for (int x=0; x&lt;512; x++) {</div>
<div>            for (int y=0; y&lt;512; y++) {</div><div>                if ((useMod0 &amp;&amp; y%2 == 0) || (!useMod0 &amp;&amp; y%2 == 1))</div><div>                    image.SetScalarComponentFromDouble(x, y, 0, 0, 1);</div>
<div>            }</div><div>        }</div><div>        vtkRenderWindowPanel renWin = new vtkRenderWindowPanel();</div><div>        renWin.setInteractorStyle(new vtkInteractorStyleImage());</div><div>        vtkImageViewer2 imageViewer = new vtkImageViewer2();</div>
<div>        imageViewer.SetInput(image);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>imageViewer.SetColorLevel(0.5);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>imageViewer.SetColorWindow(1);</div>
<div>        imageViewer.SetSliceOrientationToXY();</div><div>        imageViewer.SetSlice(1);</div><div>        imageViewer.SetRenderWindow(renWin.GetRenderWindow());</div><div>        imageViewer.GetRenderer().ResetCamera();</div>
<div><br></div><div>        JPanel renWinPanel = new JPanel(new BorderLayout());</div><div>        renWinPanel.add(renWin, BorderLayout.CENTER);</div><div>        return renWinPanel;</div><div>    }</div><div><br></div><div>
    public static void main(String[] args) {</div><div>        topPanel = new JPanel(new BorderLayout());</div><div>        topPanel.setMinimumSize(new Dimension(400, 200));</div><div>        topPanel.setPreferredSize(new Dimension(400, 200));</div>
<div>        topPanel.add(createImagePanel(true), BorderLayout.CENTER);</div><div><br></div><div>        bottomPanel = new JPanel(new BorderLayout());</div><div>        bottomPanel.setMinimumSize(new Dimension(400, 200));</div>
<div>        bottomPanel.setPreferredSize(new Dimension(400, 200));</div><div>        bottomPanel.add(createImagePanel(false), BorderLayout.CENTER);</div><div><br></div><div>        JButton switchButton = new JButton(&quot;Switch Panels&quot;);</div>
<div>        switchButton.addActionListener(new ActionListener() {</div><div>            public void actionPerformed(ActionEvent e) {</div><div>                // This is where VTK is going haywire</div><div>                Component oldTop = topPanel.getComponent(0);</div>
<div>                Component oldBottom = bottomPanel.getComponent(0);</div><div><br></div><div>                topPanel.remove(oldTop);</div><div>                bottomPanel.remove(oldBottom);</div><div><br></div><div>                topPanel.add(oldBottom, BorderLayout.CENTER);</div>
<div>                bottomPanel.add(oldTop, BorderLayout.CENTER);</div><div>                frame.getContentPane().validate();</div><div>                frame.getContentPane().repaint();</div><div>            }</div><div>
        });</div><div><br></div><div>        frame = new JFrame();</div><div>        frame.getContentPane().setLayout(new BorderLayout());</div><div>        frame.getContentPane().add(topPanel, BorderLayout.NORTH);</div><div>
        frame.getContentPane().add(bottomPanel, BorderLayout.CENTER);</div><div>        frame.getContentPane().add(switchButton, BorderLayout.SOUTH);</div><div>        frame.setSize(400, 400);</div><div>        frame.setLocationRelativeTo(null);</div>
<div>        frame.setVisible(true);</div><div>    }</div><div>}</div><div><br></div><br><div class="gmail_quote">On Thu, Dec 2, 2010 at 8:10 PM, Jim Peterson <span dir="ltr">&lt;<a href="mailto:jimcp@cox.net">jimcp@cox.net</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">Jonathan Morra wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I have a java swing JPanel that contains another JPanel that contains a vtkRenderWindowPanel which is hooked up to a vtkImageViewer2. In the course of my program I want to remove the inner JPanel from the outer JPanel with JPanel.remove(). I then want to add the inner JPanel to a different outer JPanel. However when I do this VTK throws the following exception<br>

<br>
ERROR: In ..\..\src\Rendering\vtkWin32OpenGLRenderWindow.cxx, line 247<br>
vtkWin32OpenGLRenderWindow (00000004C8E4830): wglMakeCurrent failed in MakeCurrent(), error: The handle is invalid.<br>
<br>
This seems like a very simple swing operation that I&#39;m trying to do, but it&#39;s failing. Does anyone know what I&#39;m doing wrong?<br>
Thanks<br>
<br>
</blockquote></div></div>
Jonathan,<br>
you do understand that the vtk classes are JNI interfaces to the shared library version of vtk. I suspect you are causing Java to free resources that are still referenced by the memory instantiated by the still active structures outside Java in vtk. Without an example it is pretty hard to try to visualize the java and native system memory from your description. What you are doing might be sensible with all java objects, but when JNI objects are involved memory references may not be as clear as desired. are not always completely in sync.<br>

Chances are if you coded the same kind of operation in C++ the make current routine would have detected the different potential interaction source and issued this message: (from the MakeCurrent() function in vtkWin32OpenGLRenderWindow.Cxx)<br>

<br>
   if(this-&gt;IsPicking &amp;&amp; current)<br>
     {<br>
     vtkErrorMacro(&quot;Attempting to call MakeCurrent for a different window&quot;<br>
                   &quot; than the one doing the picking, this can causes crashes&quot;<br>
                   &quot; and/or bad pick results&quot;);<br>
     }<br>
<br>
Unless you want to post an example, I would say &quot;If it hurts, don&#39;t do that&quot;.<br>
<br>
Hope that helps,<br><font color="#888888">
Jim<br>
<br>
<br>
</font></blockquote></div><br></div>