ArcGIS Runtime SDK for Java

Show Labels on Layer

Demonstrates how to show labels on a feature layer.

How it works

T To show labels on a feature layer:

  1. First, create a FeatureLayer with a ServiceFeatureTable using an online feature service.
  2. Create a TextSymbol to use for displaying the label text.
  3. Create a JSON string for the label definition.
    • Set the "LabelExpressionInfo.expression" key to express what the text the label should display. You can use fields of the feature by using $feature.field_name in the expression.
    • To use the text symbol, set the "symbol" key to the symbol's JSON representation using textSymbol.toJson().
  4. Create a label definition from the JSON using LabelDefinition.fromJson(json).
  5. Add the definition to the feature layer with featureLayer.getLabelDefinitions().add(labelDefinition).
  6. Lastly, enable labels on the layer using featureLayer.setLabelsEnabled().


 * Copyright 2018 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.displayinformation.show_labels_on_layer;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


import com.esri.arcgisruntime.arcgisservices.LabelDefinition;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.TextSymbol;

public class ShowLabelsOnLayerSample 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);

      // size the stage, add a title, and set scene to stage
      stage.setTitle("Show Labels on Layer Sample");

      // create a map view and set a map
      mapView = new MapView();
      ArcGISMap map = new ArcGISMap(Basemap.createLightGrayCanvas());

      // create a feature layer from an online feature service of US Highways
      String serviceUrl = "";
      ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(serviceUrl);
      FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);

      // zoom to the layer when it's done loading
      featureLayer.addDoneLoadingListener(() -> {
        if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
        } else {
          new Alert(Alert.AlertType.ERROR, featureLayer.getLoadError().getMessage()).show();

      // use large blue text with a yellow halo for the labels
      TextSymbol textSymbol = new TextSymbol();

      // construct the label definition json
      JsonObject json = new JsonObject();
      // prepend 'I - ' (for Interstate) to the route number for the label
      JsonObject expressionInfo = new JsonObject();
      expressionInfo.add("expression", new JsonPrimitive("'I -' + $feature.rte_num1"));
      json.add("labelExpressionInfo", expressionInfo);
      // position the label above and along the direction of the road
      json.add("labelPlacement", new JsonPrimitive("esriServerLinePlacementAboveAlong"));
      // only show labels on the interstate highways (others have an empty rte_num1 attribute)
      json.add("where", new JsonPrimitive("rte_num1 <> ' '"));
      // set the text symbol as the label symbol
      json.add("symbol", new JsonParser().parse(textSymbol.toJson()));

      // create a label definition from the JSON string
      LabelDefinition labelDefinition = LabelDefinition.fromJson(json.toString());
      // add the definition to the feature layer and enable labels on it

      // add the map view to stack pane

    } catch (Exception e) {
      // on any error, display stack trace

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

    // release resources when the application closes
    if (mapView != null) {

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


In this topic
  1. How it works
  2. Code