# Specifying caption text for a DataGrid

ID: FLASH31

Technology: flash

Type: Technique

## When to Use

- Adobe Flash Professional version MX and higher
- Adobe Flex

## Description

The objective of this technique is to programmatically associate captions for DataGrids where captions are provided in the presentation. Normally, the caption for a table is a table identifier and acts like a title or heading for the table.

Flash does not have a caption element for the DataGrid component, but the same effect can be achieved through the following approach:

1. Place a label component or textfield above the DataGrid, containing the grid's caption text.
2. Duplicate the caption text and add it as the grid's accessible name. This can either be achieved by setting a value to the grid's "name" field in the Accessibility panel or by setting the grid's `AccessibilityProperties.name` property.

## Examples

### Associating a label with a DataGrid

This is an example of a DataGrid being added to the stage in Flash Professional from the Components panel. A label element is also added from the Components panel to contain the caption text and the caption text is used in the Accessibility control panel in Flash to serve as the accessibility name for the DataGrid.

- Create a new Flash file (.fla) or open an existing one to put a DataGrid into.
- Open the Flash components panel from the Window menu
- Drag a DataGrid component onto the stage and position as desired.
- Drag a label component onto the stage and position as desired.
- Add text to the label component.
- Select the DataGrid component and add the same text as is used in the label component to the name field for DataGrid, using the Accessibility control panel.

### Associating a caption with a DataGrid using ActiveScript 3

This is a basic AS3 example of a DataGrid generated through scripting. Additionally a label element is created, containing the caption text, and the caption text is added to the grid as an accessible name.

    import fl.accessibility.DataGridAccImpl;
    import fl.controls.DataGrid;
    import fl.controls.Label;
    import fl.data.DataProvider;
    import flash.accessibility.Accessibility;
    import flash.accessibility.AccessibilityProperties;
    import flash.system.Capabilities;

    // enable accessibility for the DataGrid
    DataGridAccImpl.enableAccessibility();

    createGrid();

    // set the data grid caption text
    var gridCaptionText: String = "Game Results";
    gridCaption.text = gridCaptionText;
    //add the caption text as the DataGrid's accessible name
    var accProps: AccessibilityProperties = new AccessibilityProperties();
    accProps.name = gridCaptionText;
    aDg.accessibilityProperties = accProps;
    if (Capabilities.hasAccessibility)
    Accessibility.updateProperties();

    function createGrid() {

      //create and add the components
      var aDg: DataGrid = new DataGrid();
      var gridCaption: Label = new Label();
      addChild(aDg);
      addChild(gridCaption);
      aDg.move(50, 50);
      gridCaption.move(50, 20);

      var captionFormat: TextFormat = new TextFormat();
      captionFormat.size = 24;
      gridCaption.setStyle("textFormat", captionFormat);
      gridCaption.width = 300;
      gridCaption.height = 100;
      bldRosterGrid(aDg);
      //prepare the data
      var aRoster: Array = new Array();
      aRoster =[
        {Name: "Wilma Carter", Bats: "R", Throws: "R", Year: "So", Home: "Redlands, CA"},
        {Name: "Sylvia Munson", Bats: "R", Throws: "R", Year: "Jr", Home: "Pasadena, CA"},
        {Name: "Carla Gomez", Bats: "R", Throws: "L", Year: "Sr", Home: "Corona, CA"},
        {Name: "Betty Kay", Bats: "R", Throws: "R", Year: "Fr", Home: "Palo Alto, CA"},
      ];
      aDg.dataProvider = new DataProvider(aRoster);
      aDg.rowCount = aDg.length;
    };

    function bldRosterGrid(dg: DataGrid) {
      dg.setSize(400, 300);
      dg.columns =[ "Name", "Bats", "Throws", "Year", "Home"];
      dg.columns[0].width = 120;
      dg.columns[1].width = 50;
      dg.columns[2].width = 50;
      dg.columns[3].width = 40;
      dg.columns[4].width = 120;
    };

Notes on this code sample:

- For a demonstration, see the working version of Associating a caption with a DataGrid using ActiveScript 3. The source of Associating a caption with a DataGrid using ActiveScript 3 is available.
- The accessible name can also be applied to the DataGrid using the Accessibility panel in the Flash authoring tool.
- In the above example, the text used for the DataGrid caption will be read twice, once as the text label that is offered for sighted users, and again as the accessible name for the DataGrid. Authors can avoid duplicate voicing by setting the `silent` property for the label text to `true`.

## Tests

### Procedure

1. Check whether the Flash movie contains a DataGrid component.
2. Confirm that each DataGrid's caption text has been added to the component as an accessible name.

### Expected Results

Step 2 is true.

## Related Techniques

- H39