arcgissamples\addins\dockablewindow\DockableMap.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Simple add-in in ArcMap
arcgissamples\addins\dockablewindow\DockableMap.java
/* Copyright 2015 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.addins.dockablewindow;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import com.esri.arcgis.addins.desktop.DockableWindow;
import com.esri.arcgis.arcmapui.IMxDocument;
import com.esri.arcgis.framework.IApplication;

public class DockableMap extends DockableWindow {
  
  DefaultListModel listmod;
  JList list;
  JButton button;
  JPanel panel;
  IApplication app;
  IMxDocument mxDoc;
  
  // Create the dockable window's user interface 
  @Override
  public Component createUI() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    button = new JButton("Show/Update LayerList");
    panel = new JPanel();
    button.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        listmod = new DefaultListModel();
        list = new JList(listmod);
        try{
          // Get the hold to the map document from the applcation that you get from the init() method
          mxDoc = (IMxDocument)app.getDocument();
          int layerCount = mxDoc.getFocusMap().getLayerCount();
          if(layerCount == 0 ){
            JOptionPane.showMessageDialog(button.getParent(), "Please add a mapdocument");
          }
          for(int i=0;i<layerCount;i++){
            listmod.addElement(mxDoc.getFocusMap().getLayer(i).getName());
          }
          panel.add(list, BorderLayout.CENTER);
          list.updateUI();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }});
    panel.setLayout(new BorderLayout());
    panel.add(button, BorderLayout.NORTH);
    return panel;
  }

  // Initializes this dockable window with the ArcGIS application it is hosted in
  public void init(IApplication app) {
    this.app = app;
  }
  
}