Skip To Content ArcGIS for Developers Sign In Dashboard

List Related Features


 * Copyright 2017 Esri.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.

package com.esri.samples.featurelayers.list_releated_features;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Alert;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.DrawStatus;
import com.esri.arcgisruntime.mapping.view.MapView;

public class ListRelatedFeaturesSample extends Application {

  private MapView mapView;

  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("List Related Features Sample");

      // show a progress indicator while the map loads
      ProgressIndicator progressIndicator = new ProgressIndicator();
      progressIndicator.setMaxSize(25, 25);

      // create an accordion view for displaying the related features according to their feature table
      Accordion accordion = new Accordion();
      accordion.setMaxSize(200, 300);

      // use the Alaska National Parks and Preserves Species web map
      ArcGISMap map = new ArcGISMap("");

      // add the map to the map view
      mapView = new MapView();

      // make selection outline yellow (0xFFFFFF00)

      // hide the progress indicator when the layer is done drawing
      mapView.addDrawStatusChangedListener(drawStatusChangedEvent -> {
        if (drawStatusChangedEvent.getDrawStatus() == DrawStatus.COMPLETED) {

      // wait until the map is done loading
      map.addDoneLoadingListener(() -> {
        // get the first feature layer for querying
        FeatureLayer featureLayer = (FeatureLayer) map.getOperationalLayers().get(0);

        mapView.setOnMouseClicked(event -> {
          // check for primary or secondary mouse click
          if (event.isStillSincePress() && event.getButton() == MouseButton.PRIMARY) {

            // create a point from where the user clicked
            Point2D point = new Point2D(event.getX(), event.getY());

            // convert to map coordinate
            Point mapPoint = mapView.screenToLocation(point);

            // identify the clicked features
            QueryParameters queryParameters = new QueryParameters();
            queryParameters.setGeometry(GeometryEngine.buffer(mapPoint, 10));
            final ListenableFuture<FeatureQueryResult> selectFeatureQuery = featureLayer.selectFeaturesAsync
                (queryParameters, FeatureLayer.SelectionMode.NEW);
            selectFeatureQuery.addDoneListener(() -> {

              try {
                FeatureQueryResult result = selectFeatureQuery.get();
                // get the first selected feature
                Iterator<Feature> iterator = result.iterator();
                if (iterator.hasNext()) {
                  ArcGISFeature selectedFeature = (ArcGISFeature);
                  // get the feature's feature table
                  ArcGISFeatureTable featureTable = selectedFeature.getFeatureTable();

                  // query related features
                  final ListenableFuture<List<RelatedFeatureQueryResult>> relatedFeatureQuery = featureTable
                  relatedFeatureQuery.addDoneListener(() -> {
                    try {
                      //clear previous results
                      // add all related features (grouped) into panes of the accordion
                      List<RelatedFeatureQueryResult> results = relatedFeatureQuery.get();
                      for(RelatedFeatureQueryResult relatedFeatureQueryResult : results){
                        ListView<String> featureList = new ListView<>();
                        String relatedTableName = relatedFeatureQueryResult.getRelatedTable().getTableName();
                        // create a pane for the feature table with a list for its features
                        TitledPane tablePane = new TitledPane(relatedTableName, featureList);
                        for(Feature relatedFeature : relatedFeatureQueryResult) {
                          // show the related feature with its display field value in the list
                          ArcGISFeature feature = (ArcGISFeature) relatedFeature;
                          String displayFieldName = feature.getFeatureTable().getLayerInfo().getDisplayFieldName();
                          String displayFieldValue = feature.getAttributes().get(displayFieldName).toString();
                      //expand the accordion's last pane to show the related features
                      accordion.setExpandedPane(accordion.getPanes().get(accordion.getPanes().size() - 1));
                    } catch (InterruptedException | ExecutionException e) {
                      Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to get related features");

              } catch (InterruptedException | ExecutionException e) {
                Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to get identify the selected feature");

      // add the map view and accordion view to stack pane
      stackPane.getChildren().addAll(mapView, accordion, progressIndicator);
      StackPane.setAlignment(accordion, Pos.TOP_LEFT);
      StackPane.setAlignment(progressIndicator, Pos.CENTER);
    } catch (Exception e) {
      // on any error, display the stack trace.

   * Stops and releases all resources used in application.
  public void stop() {

    if (mapView != null) {

   * Opens and runs application.
   * @param args arguments passed to this application
  public static void main(String[] args) {



In this topic
  1. Code