Adding an item to a Jewel List

Adding an item to a Jewel List

Adding an item to a Jewel List 607 555 Carlos Rovira

In this example we’ll cover how to set up a Jewel List control that shows a list of basic string data in a Royale application, and then add an item to the list. It uses the new Jewel UI set that supports themes and is available in the 0.9.4 release or later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
<j:Application xmlns:fx="//ns.adobe.com/mxml/2009"
              xmlns:j="library://ns.apache.org/royale/jewel"
              xmlns:js="library://ns.apache.org/royale/basic"
              xmlns:html="library://ns.apache.org/royale/html">
   
    <fx:Script>
        <![CDATA[
           private function changeHandler(event:Event):void {
               selected.text = "Selected: " + list.selectedItem;
           }
           
           private function clickHandler(event:Event):void {
               avengersCharacters.addItem("Hawkeye");
           }
        ]]>
    </fx:Script>

    <j:initialView>
        <j:View>
            <j:beads>
                <j:VerticalLayout gap="10"/>
            </j:beads>
           
            <html:H3 text="Avengers Character List"/>
   
            <j:List id="list" width="200" height="300" change="changeHandler(event)">
                <j:beads>
                    <j:AddListItemRendererForArrayListData/>
                </j:beads>
                <j:dataProvider>
                    <js:ArrayList id="avengersCharacters" source="[Iron Man, Hulk, Thor, Captain America, Black Widow]" />
                </j:dataProvider>
            </j:List>
            <j:Label id="selected"/>

            <j:Button width="200" emphasis="primary" text="who is missing?" click="clickHandler(event)"/>
        </j:View>
    </j:initialView>

</j:Application>

In this example, the List is populated from an ArrayList object that holds a basic array of the data. The ArrayList has all collection methods to manage internal data, like addItem and addItemAt. We used Avengers character names to showcase this example, but you’ll see one character is missing ;).
For this example we used the Amethyst Jewel Theme to match movie colors better.

When you select a row in the List a CHANGE event is fired. We have the event call an event handler that shows the data for the selected item in a label.

Finally you can click the “Who’s missing?” button to add the missing Avenger to the list. The List will update to reflect the addition. For this to happen you’ll need to use a bead that encapsulates the code responsible of adding the item to the data provider and create the corresponding item renderer called AddListItemRendererForArrayListData.

The result of this code snippet is the following:

(We’re using an iframe to host the compiled results of this example. To see the example in a separate window click this link.)

Full project with source code can be found here:

Join Us!

You can be part of Apache Royale and start using and contributing to this project. We need your skills to make this great technology reach the next step. You can start by joining us in our mailing list.