arcgissamples\addins\button\AddLayerButton.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Simple add-in in ArcMap
arcgissamples\addins\button\AddLayerButton.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.
* 
*/
/* Copyright 2012 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.1/userestrictions.txt.
* 
*/
package arcgissamples.addins.button;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import arcgissamples.addins.util.ShapeFileFilter;

import com.esri.arcgis.addins.desktop.Button;
import com.esri.arcgis.arcmapui.IMxDocument;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.framework.IApplication;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;

public class AddLayerButton extends Button {
  
  IApplication app;
  IMxDocument mxDoc;

  // Returns whether this button is checked
  public boolean isChecked() {
    return false;
  }

  // Returns whether this button is enabled
  public boolean isEnabled() {
    return true;
  }
  
  // This is called when the button is clicked 
  public void onClick() {
    try {
      SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
          try {
            addLayer();
          } catch (Exception ex) {}
        }
        
      });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  // This initializes the button and gets a reference to the hosting ArcGIS application 
  public void init(IApplication app) {
    this.app = app;
  }

  // This will open a JFileChooser and lets you to select data and add to the map.
  public void addLayer() throws Exception{
    IWorkspaceFactory workspaceFactory = null; 
        workspaceFactory = new ShapefileWorkspaceFactory();
        IFeatureWorkspace workSpace = null; 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFileChooser fileChooser = new JFileChooser();
      // Create a filter only to show .shp files in file chooser
        ShapeFileFilter filterSHP = new ShapeFileFilter();
      filterSHP.addExtension("shp");
      fileChooser.setFileFilter(filterSHP);
      int returnVal = fileChooser.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          workSpace = (IFeatureWorkspace) workspaceFactory.openFromFile(file.getParent(), 0);
          IFeatureClass featureClass = null; 
          featureClass = workSpace.openFeatureClass(file.getName().substring(0,(file.getName().length()-4)));
          IFeatureLayer layer = null; 
          layer = new FeatureLayer();
          layer.setFeatureClassByRef(featureClass);
          layer.setName(featureClass.getAliasName());
      mxDoc = (IMxDocument) app.getDocument();
          mxDoc.getFocusMap().addLayer(layer);
          mxDoc.getActiveView().partialRefresh(esriViewDrawPhase.esriViewGeography, layer, null);
      }
    }

}