Creating ArcMap addins with Java SDk

5268
19
03-20-2014 11:50 AM
KevinYanuk
Occasional Contributor
Hello,

I am trying to create a simple Dockable Window addin using the ArcObjects Java SDK, but I am having a small issue.  I am able to create the addin successfully, with a button and dockable window, but when I install the addin using the .esriAddin installer, I cannot see the addin located in the category I chose... here is my code:

button:
import java.io.IOException;

import com.esri.arcgis.addins.desktop.Button;
import com.esri.arcgis.framework.IApplication;
import com.esri.arcgis.framework.IDockableWindow;
import com.esri.arcgis.framework.IDockableWindowManager;
import com.esri.arcgis.framework.IDockableWindowManagerProxy;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.UID;


public final class btnMain extends Button
{
 private IDockableWindow docWin;

 @Override 
 public void init(IApplication app) {
     try {
         IDockableWindowManager dwm = new IDockableWindowManagerProxy(app);
         UID uid = new UID();
         uid.setValue("com.esri.arcgis.arcmap.addin.frmMain");
         docWin = dwm.getDockableWindow(uid);
     }
     catch (Exception e) {
         e.printStackTrace();
     }
 }
 
 @Override
 public void onClick() throws IOException, AutomationException {
  try {
         if (docWin != null) {
             docWin.show(!docWin.isVisible());
         }
     }
     catch (Exception e) {
         e.printStackTrace();
     }
 }
}


dockable window:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.esri.arcgis.addins.desktop.DockableWindow;
import com.esri.arcgis.arcmapui.IMxDocument;
import com.esri.arcgis.framework.IApplication;
import com.esri.arcgis.interop.AutomationException;


public final class frmMain extends DockableWindow
{
 private JButton jButton;
 private JPanel jPanel;
 private IApplication app;
 
 public static void main(String[] args){
 }

 @Override
 public Component createUI() {
  jButton = new JButton("Click Me!");
     jPanel = new JPanel();

     jButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             JOptionPane.showMessageDialog(null, "Hello, World"); 
         } 
     });
     
     jPanel.setLayout(new BorderLayout());
     jPanel.add(jButton, BorderLayout.NORTH);

     return jPanel;
 }
 
 @Override 
 public void init(IApplication app) {
     this.app = app;
     try {
   IMxDocument mxDocument = (IMxDocument)app.getDocument();
  } catch (AutomationException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}



Any help is greatly appreciated.

Thanks
0 Kudos
19 Replies
LeoDonahue
Occasional Contributor III
Two things.

1.  The command will be found in the Java Commands section under customize, like so:

[ATTACH=CONFIG]32370[/ATTACH]

2.  The id value of your dockable window, assuming you took the defaults, would be "dockablewindow1".

The sample you are looking at uses the following for the id value:  "com.esri.arcgis.arcmap.addin.arcmapdockablewindow"

That probably worked for the developer making the sample, but for people who follow the sample and don't use the same package structure, it doesn't work.

You probably missed this part:


Setting properties

A dockable window has a number of properties for you to set. The following is a list of all of the properties with an explanation for each:


  • id* - The id represents the unique name that is used to identify your dockable window. It is possible for you to create more than one dockable window for a given project and this id property is used to distinguish between the different dockable windows. Notice that the previous image shows a default value for this property. Ideally, you should replace this id with a more meaningful name. It is also highly recommended that the Java package naming convention is used when constructing your id property value. For example, com.esri.arcgis.arcmap.addin.arcmapdockablewindow could be used to represent the add-in dockable window being created for this document (Required).

0 Kudos
KevinYanuk
Occasional Contributor
Two things.

1.  The command will be found in the Java Commands section under customize, like so:

[ATTACH=CONFIG]32370[/ATTACH]

2.  The id value of your dockable window, assuming you took the defaults, would be "dockablewindow1".

The sample you are looking at uses the following for the id value:  "com.esri.arcgis.arcmap.addin.arcmapdockablewindow"

That probably worked for the developer making the sample, but for people who follow the sample and don't use the same package structure, it doesn't work.

You probably missed this part:




Thank you for your response, Leo, it was very helpful.  One quick question - is there a way to incorporate a GUI designer (like Window Builder) to design the dockable window form?
0 Kudos
LeoDonahue
Occasional Contributor III
Sure, create your GUI in window builder like you would normally.

In your Dockable Window class, and in this method:  public Component createUI()

Just instantiate your form in that method and return an instance of it.

Basically like this:

MyGUIForm mygui = new MyGUIForm();
return mygui;

There are patterns that you can follow that will give you only one instance of that of form, so that you don't get new instances of the MyGUIForm every time you click the button.
0 Kudos
KevinYanuk
Occasional Contributor
Sure, create your GUI in window builder like you would normally.

In your Dockable Window class, and in this method:  public Component createUI()

Just instantiate your form in that method and return an instance of it.

Basically like this:

MyGUIForm mygui = new MyGUIForm();
return mygui;

There are patterns that you can follow that will give you only one instance of that of form, so that you don't get new instances of the MyGUIForm every time you click the button.



Awesome, thanks so much, you've been a big help.
0 Kudos
LeoDonahue
Occasional Contributor III
Just to be a little more specific.  I would stick to extending JPanel rather than a JFrame in your custom GUI. 

While the API says that createGUI() will take a Swing or AWT component, I'm not sure that you would need a JFrame and the window events that go along with that inside a DockableWindow, since the DockableWindow has a dispose() method.
0 Kudos
KevinYanuk
Occasional Contributor
Just to be a little more specific.  I would stick to extending JPanel rather than a JFrame in your custom GUI. 

While the API says that createGUI() will take a Swing or AWT component, I'm not sure that you would need a JFrame and the window events that go along with that inside a DockableWindow, since the DockableWindow has a dispose() method.



**EDIT** -> now it looks like when I install the addin (just a button right now), it is not showing up in my Addin-Manager...

Leo,

I came this morning and followed your instructions... however, I can't a "Java Commands" category in ArcMap:
[ATTACH=CONFIG]32393[/ATTACH]

I had left the category in the eclipse designer:
[ATTACH=CONFIG]32394[/ATTACH]


Any ideas?
0 Kudos
LeoDonahue
Occasional Contributor III
Well, I was wrong about being able to instantiate an instance of another class that extends JPanel from within the createGUI() method.

So today we learned that the createGUI() method of DockableWindow will return a component that is a JPanel as long as the code lives in the createGUI() method, but it is unhappy to allow an instance created of a JPanel or even Component from another class.

Just create your JPanel in WindowBuilder and copy the code into the createGUI() method of DockableWindows.



As for your Java Commands problem.

See this:
[ATTACH=CONFIG]32405[/ATTACH]
0 Kudos
KevinYanuk
Occasional Contributor
Well, I was wrong about being able to instantiate an instance of another class that extends JPanel from within the createGUI() method.

So today we learned that the createGUI() method of DockableWindow will return a component that is a JPanel as long as the code lives in the createGUI() method, but it is unhappy to allow an instance created of a JPanel or even Component from another class.

Just create your JPanel in WindowBuilder and copy the code into the createGUI() method of DockableWindows.



As for your Java Commands problem.

See this:
[ATTACH=CONFIG]32405[/ATTACH]



Hadn't changed it (dark eclipse theme):

[ATTACH=CONFIG]32448[/ATTACH]
0 Kudos
KevinYanuk
Occasional Contributor
EDIT: I was able to get the Java Commands category to show up once using 1.6, but have not been able to reproduce it... I tried remaking the addin using the same steps, and it is missing once again... below are my steps using JRE 1.6:

[ATTACH=CONFIG]32689[/ATTACH]
[ATTACH=CONFIG]32690[/ATTACH]
[ATTACH=CONFIG]32691[/ATTACH]
[ATTACH=CONFIG]32692[/ATTACH]
[ATTACH=CONFIG]32693[/ATTACH]
0 Kudos