JMap Failed to create map context on second add

965
3
Jump to solution
12-09-2016 06:58 AM
MichaelKnight
New Contributor II

The following example code repeatedly adds and removes a JMap from a JFrame. At first the JMap shows without issue but it throws an Excetpion when trying to add a map for a second time. (java.lang.RuntimeException: Failed to create map context.)

The code included works fine when adding and removing regular swing components you can make the sample application work with a JButton instead by setting the button boolean at the top of the class to true. I believe this shows that the code *should* be fundamentally sound in its approach.

Would anyone be able to shed any light on why we are seeing an exception when doing this? 

PS: The solution cannot be to keep a reference to a single map and just hide and show it I am specifically looking to delete and recreate the map every time.

0 Kudos
1 Solution

Accepted Solutions
ColinAnderson1
Esri Contributor

Hi,

I think your problem is that the first JMap you create is not created on the Swing UI thread. If you do your first addMap like SwingUtilities.invokeAndWait(() -> addMap()) your app should work ok. Remember as a Swing component JMaps should only be created/accessed on the UI thread. 

Colin

View solution in original post

3 Replies
MichaelKnight
New Contributor II

Hi Guys, 

I have actually found that if you create a new JMap as a member of this class but never show it so just add the following line of code below the boolean flag it works.

JMap bmap = new JMap();

When all references to the ArcGISRuntime SDK are removed from memory does it try to unload the associated JNI dlls from the java process? Could it be failing in the first instance when it is reloading them for a second time?

0 Kudos
ColinAnderson1
Esri Contributor

Hi,

I think your problem is that the first JMap you create is not created on the Swing UI thread. If you do your first addMap like SwingUtilities.invokeAndWait(() -> addMap()) your app should work ok. Remember as a Swing component JMaps should only be created/accessed on the UI thread. 

Colin

MichaelKnight
New Contributor II

Hi Collin,

Thanks for pointing that out. Its never been a problem with Javas swing components but I understand technically that would be the correct thing to do. Your suggestion fixes the issue. Thanks for the quick response on this one.