How to create an add-in combo box


Summary This topic guides you through the workflow for creating a combo box using the Eclipse integrated development environment (IDE). After the workflow is presented, the topic takes a closer look at the abstract ComboBox class and defines additional methods available to you when creating your combo box.

In this topic


About the combo box

A combo box provides a set of choices, visually seen as a drop-down box, from which a selection can be made and acted upon. A combo box can also be enabled to allow an end user to add a value in the combo box and acted on accordingly. The scale combo box in ArcMap is a good example of a combo box. When data with a known coordinate system is added to ArcMap, the scale combo box is enabled giving the user a set of pre-defined scales from which to choose. It also allows a user to type a desired scale not present in the list of options and the map is displayed based on the value added.
The following guides you through the process of creating a combo box using the Eclipse IDE. Before beginning this workflow, make sure that you have created an ArcMap add-in project using Eclipse. For more information, see How to create an add-in project in Eclipse. Since there is no difference between creating a combo box for any one of the ArcGIS Desktop applications, this workflow shows you how to create a combo box for ArcMap.
The workflow for creating a combo box in Eclipse consists of the following (done in the order show):

Creating a combo box

The following shows you how to create a combo box for an existing Eclipse add-in project. Ensure that you have the add-in view enabled for the add-in editor on the config.xml file and that you have completed the required overview properties. See the following screen shot: 

  1. Under All add-ins, click the Add button. The Create New Add-In dialog box appears. See the following screen shot:

  1. Select the Combo Box option on the preceding screen shot (there are eight different options), then click OK.

    A new section of the add-in editor appears with various properties for you to set for your new combo box. By default, the id*, class*, caption*, and category* are completed with default values to help expedite the development process. Also, observe the warning symbol under the Combo Box Details and next to the class* property. This indicates that the Java class for your new combo box has not been created. In a later step in this workflow, you will learn how to create your Java class and where you will write your business logic. For now, the following screen shot shows you the new Combo Box Details section that is added to the editor with default values:

Setting properties

A combo box 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*—Represents the unique name that is used to identify your combo box. You can create more than one combo box for a project and this id property is used to distinguish between the different combo boxes. The previous screen shot shows a default value for this property. Ideally, you should replace this id with a more meaningful name. Use the Java package naming convention when constructing your id property value. For example, com.esri.arcgis.arcmap.addin.arcmapcombobox could be used to represent the add-in combo box being created for this topic (required).
  • class*—Identifies the Java class for your combo box. The Java class is where you write your business logic. This class is important because it is called when the combo box is used in a desktop application. To create a class, click the class property. Notice that the class property has a link (blue and underlined) to indicate this. Use the Java package naming convention when constructing your class. For example, com.esri.arcgis.addin.ArcMapComboBox could be used to represent the combo box class being created (required).
  • caption*—Applies a name to the combo box and is used in two different locations after the project is deployed (required). The first location is the Add-In Manager where the caption is used as metadata to help a user identify available types. The following screen shot uses a value of ArcMap Combo Box for the caption property and is exposed in the Add-In Manager (the type of add-in is identified in parentheses):



    The second place that the caption is used is when a user is adding the combo box to the user interface (UI) of a desktop application. For this workflow, see How to deploy your add-in. For reference, the Customize dialog box Commands tab is used and will use the caption defined under the commands text box. See the following screen shot:

  • category*—Applies a name that can identify your add-ins as a group. A category can have more than one add-in by supplying the same name for the category property in each defined customization. For example, you could have an ArcMap add-ins category that can contain all your customizations for ArcMap, such as, a button, tool, toolbar, and so on. To achieve this, give the same category value for each add-in type (required).

    Identical to the caption property, the category property is visible in two locations. The first location is the Add-In Manager (notice that the Category explicitly identifies this property). See the following screen shot:



    The second location the category is used, is when a user is locating the add-in project on the Customize dialog box in a desktop application. The Customize dialog box Commands tab lists a number of categories (for example, Animation, ArcScan, and so on), and the category property value you set is used in this list to identify your type of add-in. See the following screen shot:

  • image—16 by 16 pixel image that can be used to represent your combo box. This image is used on the Customize dialog box Commands tab to symbolize your combo box. This image is not used by default in the desktop application to which the combo box is added. Instead, a drop-down menu is used to represent a combo box. Place the image within an image folder in your project and the Browse button allows you to add a reference to this image (optional).
  • tooltip—Brief description of your combo box that can be used to intuitively help your end user use your combo box or remind them of the combo box's purpose. The ToolTip appears when the mouse pointer pauses over the combo box in a desktop application. A message dialog box then shows the message supplied for this property (optional).
  • message—More detailed description of your combo box that describes what the combo box does. Identical to the ToolTip, the message appears when the mouse pointer pauses over the combo box in a desktop application in conjunction with the ToolTip. This message appears with the value supplied with this property in the status bar of a desktop application, which is located in the lower left corner (optional).
  • width—Represents the length of the combo box by using any character and repeating that character to represent the number of characters visible in the combo box. For example, if you want the combo box to show five characters, the width property will have a value of, XXXXX for any character (optional).
  • item width—Defines the combo box's drop-down width. When a user clicks the down arrow on the combo box, the specified item width will be used to represent the length of this drop-down box. Similarly to the width property, the item width's value is represented with any character, repeated the number of times desired for the length of the drop-down box (optional).
  • row—Determines the number of rows shown in the combo box drop-down list. This property is specified using an integer value (optional).
  • hint—Defines the unavailable text shown on the comb box when it is not active in the desktop application. This text can be used to educate the user about the types of items they select from or about the kind of items they need to select from the combo box (optional).
  • editable—Check box that indicates if the combo box allows a user to add a value into the combo box. By default, this option is turned off and the user is unable to add their item into the combo box. However, if you select the check box, the user is allowed to add their value.
  • display caption in toolbar—Check box that indicates if the caption* property shows in conjunction with the combo box. By default, this option is turned off and only the combo box appears in the desktop application. However, if you select the check box, the caption* property shows to the left of the combo box when added to the toolbar. The following screen shot shows an example of the caption property shown next to the combo box in ArcMap (the caption property value is ArcMap Combo Box on this screen shot example):

The Help Content section on the Combo Box details a portion of the editor for the help content you can supply a user with. These properties allow you to supply information that will be used when a user invokes context-sensitive Help. These are generally short pop-up topics that remain on-screen until a user clicks somewhere else.
The following properties comprise this context-sensitive Help:
  • Heading—Indicates what the text is about (optional).
  • Content—Where you place the content for the combo box's context-sensitive Help (optional).

Creating a Java class and defining your business logic

At this stage, you have finished adding values for all of the properties needed to define your combo box. The final step in this workflow, is to create a Java class that contains your business logic. Do the following steps to create the Java class:
  1. Click the class* property under the Combo Box details. A Java class dialog box appears with a few pre-populated form boxes. The one of interest is the superclass property. This property is unavailable automatically because the class that you create must inherit the abstract class ComboBox from the com.esri.arcgis.addins.desktop package. This abstract class is used to hide the implementation details for making the combo box work with a desktop application.
  2. Add a name for the new Java class file and click Finish.
In this instance, a class called ArcMapComboBox, is similar to the one in the following code example:
[Java]
public class ArcMapComboBox extends ComboBox{
    @Override public void initialize(){}
    @Override public void onEditChange(String editString){}
    @Override public void onEnter(){}
    @Override public void onFocus(boolean setFocus){}
    @Override public void onSelChange(int cookie){}
}
The generated class file has two significant pieces. The first is the use of the extends keyword to inherit from the abstract class ComboBox. The second is the five auto-generated methods, as previously shown. The following describes each of these methods, which are required:
  • initialize()—Invoked when the defined comb box is initialized by the desktop application. A typical use case for this method is to populate the combo box with options. To achieve this, another method, add(), can be used. For example, the following initialize() method will add two options to the combo box on initialization:
[Java]
private int option1;
private int option2;
@Override public void initialize(){
    try{
        option1 = add("Option 1");
        option2 = add("Option 2");
    }
    catch (Exception e){}
}
The source code assigns a result for each add method to an integer variable. This is done because the add method returns a unique ID (cookie) for each item that is added to the combo box. The returned value is important for determining which option is selected by the user in another method you must define.
  • onEditChange()—Only applicable when the editable property is enabled by selecting the check box on the add-in editor (see previous instructions for more information on this property). This method is invoked by the ArcGIS framework every time a single character is added in the edit box. You can use this method to write your business logic to respond to any character that is entered.
  • onSelChange()—The onSelChange() method is called by the ArcGIS framework every time a new selection is made on the combo box. For example, the initialize() method adds two options to the combo box, and the following code example can be used to capture that event:
[Java]
@Override public void onSelChange(int cookie){
    if (cookie == option1)
        JOptionPane.showMessageDialog(null, "Option 1 selected.");
    else if (cookie == option2)
        JOptionPane.showMessageDialog(null, "Option 2 selected.");
}
The cookie variable that is passed into this method represents the option that is selected by the user. In the initialize() method, when adding the options, these values are reported back to you. The two instance variables are used here to test which option is selected by the user and reports that fact. Based on the option that a user selects, you can utilize decision logic to execute your business logic accordingly.
  • onFocus()—Called by the ArcGIS framework when the combo box gets focus or when the combo box loses focus. A Boolean variable, setFocus, is passed into this method to indicate combo box's state. The variable is set to true when the combo box gets the focus, and false otherwise.
  • onEnter()—Called by the ArcGIS framework only when the user presses the Enter key on the edit box of the combo box. This method allows you to wait for the user to completely finish typing a value before attempting to process your business logic based on the added value. The Enter key is a way for your user to let your business logic know that they have added a value.
The previous code example prints a message dialog box outputting which option was selected by a user, but could be developed further as the application requires. For instructions on how to deploy the add-in combo box, see the previously referenced topic, How to deploy your add-in.

About the abstract class ComboBox

The abstract class ComboBox provides you with additional methods that can be overridden to help you define the required methods. Together, six of these methods exist and are described as follows.

isEnabled() method

This method allows you to add some logic to specify in what state the desktop application should be in for the combo box to be enabled, and thus allows someone to select from the list of options or type their value. An ArcGIS framework system event is called to check if a combo box is enabled or not.

add() method

The add() method allows you to add items to your combo box. Pass in the string you want to appear on the combo box. Also, this method returns an integer representing the unique value for your option. This integer is useful in other methods when determining which item is currently selected or chosen by the end user. This method is final and cannot be overridden.

remove() method

The remove() method allows you to delete items from your combo box. When calling this method, you need the ID of the item for it to be removed. This method is final and cannot be overridden.

clear() method

The clear() method allows you to clear all the elements from the combo box. This method is final and cannot be overridden.

select() method

The select() method allows you to select an element in the combo box. This can be useful if you want to initialize the combo box with a default value applied. The method accepts a cookie argument that represents the ID of your combo box item. This method is final and cannot be overridden.

getSelected() method

The getSelected() method allows you to get the selected item from the combo box. This method returns the ID that represents the item that has been selected by the user. This method is final and cannot be overridden.

Understanding the config.xml

When setting all of the properties, the config.xml file was being generated behind the scenes. To understand more about how this config.xml file is created, see Understanding the config.xml.

Creating different types of customizations

The following topics showed you how to create a combo box. However, there are additional types of add-ins that can be created and defined. 






Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo