Hello again:
I have actually found a solution to your code issue in this thread.
Now, if you are trying to add the DataFrame from the map that resides on the local hard drive and add it to the currently running session of ArcMap, then the following code will work for you:
//Instance variables of the add-in button's class definition.
IApplication application;
IMxDocument mxDocument;
IMapDocument mapDocument;
String documentPath = "C:\\temp\\StatesMap.mxd";
//Save the application variable.
@Override
public void init(IApplication app) throws IOException, AutomationException {
application = app;
}
//This code is in a private method inside my Add-In button that is called from the button's onClick()
//method
private void addDataFrame() throws Exception{
mxDocument = (MxDocument) application.getDocument();
mapDocument = new MapDocument();
mapDocument.open(documentPath, null);
Map map = (Map) mapDocument.getMap(0);
mxDocument.getMaps().add(map);
mxDocument.updateContents();
}
When I deployed the add-in button that had this source code and tested in ArcMap, it added the DataFrame to the existing map document and the table of contents was updated accordingly. I simply had to activate the new DataFrame in ArcMap in order to see the layer that was added to the .mxd on disk.
Now, if you wanted to add the layer to the active DataFrame in ArcMap instead of adding a brand new DataFrame, it's only a couple additional lines of code (see next snippet):
//This code is in a private method inside my Add-In button that is called from the button's onClick()
//method
private void addDataFrame() throws Exception{
mxDocument = (MxDocument) application.getDocument();
mapDocument = new MapDocument();
mapDocument.open(documentPath, null);
Map map = (Map) mapDocument.getMap(0);
ILayer layer = map.get_layer(0);
mxDocument.getActiveView().getFocusMap().addLayer(layer);
mxDocument.updateContents();
}
Please try the above modifications to your source code and retest. Let me know if you run into any additional issues.
Thanks and I hope this updated source code works for you.
-=Steve