Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time

#CN1ML Tag Reference


  1. Overview
  2. HTML Tags
  3. Layouts
  4. Template Attributes and Parameters
  5. Client Properties
  6. Custom Initialization: Script tags
  7. Imports
  8. UIIDs
  9. Tables
  10. Images
  11. Text
  12. Lists
  13. Tabs
  14. Scripts
  15. Localization
  16. Setter Attributes


In CN1ML, you can use any HTML (or non-HTML) tag in your document. The default Component that will be used to represent a tag is com.codename1.ui.Container, but some tags are handled specially and will be converted to a more appropriate Component (e.g. <label> is converted to com.codename1.ui.Label, etc..).

In addition, you can use the class attribute on any tag to specify a particular Component subclass.

This document outlines the rules that are used by the CN1ML parser for converting HTML tags.

##HTML Tags

HTML Tag Component Since
<label> com.codename1.ui.Label 0.1
<label class="SpanLabel"> com.codename1.ui.SpanLabel 0.1.6
<button> com.codename1.ui.Button 0.1
<textarea> com.codename1.ui.TextArea 0.1.6
<input type="text"> com.codename1.ui.TextField 0.1
<input type="password"> com.codename1.ui.TextField with PASSWORD constraint 0.1.6
<input type="checkbox"> com.codename1.ui.CheckBox 0.1
<input type="radio"> com.codename1.ui.RadioButton 0.1
<select> com.codename1.ui.ComboBox 0.1
<select size="5"> com.codename1.ui.List 0.1
<img> com.codename1.ui.Label 0.1
<table> com.codename1.ui.Container 0.1
<tabs> com.codename1.ui.Tabs 0.1
<div> com.codename1.ui.Container 0.1
<script> Converted to Java code block 0.1
<body> com.codename1.ui.Container -- this is the root 0.1

All tags not listed in the above table are converted to Container objects, except in cases where the tag is a child of a "special component" that overrides the behaviour of its children (like a <table> with its <tr> tags), and in cases where the class attribute explicitly specifies which component class to use.


Use the layout attribute on any tag (that converts to a Container or subclass thereof) to specify the layout manager that should be used with the container. The default layout, if this is omitted, will be FlowLayout.


 <div layout="BorderLayout">...</div>
 <div layout="GridLayout(3,3)">...</div>
 <div layout="BoxLayout(BoxLayout.Y_AXIS)">...</div>
 <div layout="com.mycompany.layouts.MyCustomLayout">...</div>

Some common layouts have short-cuts:

Layout ShortCut Example
BoxLayout(BoxLayout.X_AXIS) X_AXIS <div layout="X_AXIS">...</div>
BoxLayout(BoxLayout.Y_AXIS) Y_AXIS <div layout="Y_AXIS">...</div>
BoxLayout(BoxLayout.X_AXIS) x <div layout="x">...</div>
BoxLayout(BoxLayout.Y_AXIS) y <div layout="y">...</div>

###Layout Constraints

Some layout managers require a constraint when adding child components. E.g. When adding a component to a Container that uses BorderLayout, you need to specify one of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.CENTER, etc... as the first parameter of addComponent() so that it knows where to place the component.

Use the layout-constraint attribute on the child tag to specify this constraint.


 <div layout="BorderLayout">
    <div layout-constraint="BorderLayout.NORTH">North panel</div>
    <div layout-constraint="BorderLayout.CENTER">Center panel</div>

Some common constraints have shortcuts:

Constraint Shortcut Example
BorderLayout.NORTH NORTH <div layout-constraint="NORTH">...</div>
BorderLayout.SOUTH SOUTH <div layout-constraint="SOUTH">...</div>
BorderLayout.CENTER CENTER <div layout-constraint="CENTER">...</div>
BorderLayout.WEST WEST <div layout-constraint="WEST">...</div>
BorderLayout.EAST EAST <div layout-constraint="EAST">...</div>
BorderLayout.NORTH n <div layout-constraint="n">...</div>
BorderLayout.SOUTH s <div layout-constraint="s">...</div>
BorderLayout.CENTER c <div layout-constraint="c">...</div>
BorderLayout.WEST w <div layout-constraint="w">...</div>
BorderLayout.EAST e <div layout-constraint="e">...</div>

If you have a custom constraint, you can specify that using a java expression:

 <div layout-constraint="new com.mycompany.myapp.MyConstraing(1,2)">...</div>

Template Attributes/Parameters

CN1ML allows you to pass data to the template and use this data inside expressions in various element attributes. This allows you to add an extra degree of dynamism to the template. Parameters are defined by the attributes attribute of the <body> tag. They are expressed as Java Classname/VarName pairs - with multiple pairs separated by semi-colons. E.g.

<body attributes="String[] menuItems; int gridWidth; int gridHeight;">...</body>

Then, these properties can be used either inside <script> tags or as part of HTML attributes where java expressions are allowed.


 <div layout="GridLayout(gridWidth, gridHeight)">...</div>


 <select data="menuItems"></select>

####Using Templates that Require Attributes

When using a template that expects to receive attributes, you will need to provide the attribute values to the template's constructor. E.g. The template from the previous example would need to be instantiated as follows:

Map context = new HashMap();
context.put("menuItems", new String[]{"Item 1", "Item 2", "Item 3"});
context.put("gridWidth", 3);
context.put("gridHeight", 4);
MyForm f = new MyForm(context);

Putting Client Properties

The Codename One Component class includes a handy method putClientProperty that allows you to bind an arbitrary object to a UI component. CN1ML supports this by way of data-xxx attributes. If a tag includes an attribute of the form data-xxx, the value of the attribute will be added to the resulting UI component as a client property.


 <div data-playerName="Steve">...</div>

Will result in the component having something:

 cmp.putClientProperty("playerName", "Steve");

data-xxx attributes are assumed to be strings unless they are prefixed with "java:", which indicates that what follows is a java expression whose result should be added to the component's client properties.


<body attributes="com.myapp.Player player1;">
    <div name="player1ScoreBoard" data-player="java:player1">..</div>

In the above example, the template expects to receive a property named player1 of type com.myapp.Player. This is set as a client property for the Container named player1ScoreBoard.

##Custom Initialization: Script tags

Some components require custom initialization that can't be expressed with the existing HTML tags and attributes. In cases like this, you can use a <script> tag to add your own custom Java code that will be run when the UI is built.

<script>s are always executed in the context of the parent component of the script tag, and the Component will be accessible to the script via the self variable.

Special Variables in Scripts and their Meanings

Variable Meaning
self The Component that is the parent of the <script>
this The template class.
parent The parent Component of self

Some Examples


In this example, the Container that the <div> tag is transformed into will be made scrollable.

<button name="google">Google
	   self.addActionListener(new ActionListener(){
	       public void actionPerformed(ActionEvent e){

In the above example, we use a script to attach an action listener to the button. In this case we are also making use of the automatic accessors that are generated by the template -- (the getBrowser() method is the result of a tag with name="browser" somewhere else in the template).


Just like with a regular Java source file, you may find it annoying to have to use the fully-qualified class name every time you want to reference a class. CN1ML automatically imports some of the common packages (e.g. com.codename1.ui., com.codename1.ui.layouts., etc..), but you may feel the need to import others.

You can add imports to your template by adding them inside a <script> tag in the <head> of the document.


      import com.mycompany.myapp.*;


Use the uiid attribute to specify the UIID of any component. This will allow you to use the standard Codename One method for customizing the look and feel of your template. This is a nice alternative to CSS.


 <label uiid="Title">Some Title Text</label>


Codename One supports a TableLayout that allows you to layout content as a table. To set this up programmatically it is quite painful since each cell needs to have a TableLayout.Constraint created to specify the column and row settings of the cell. CN1ML simplifies this process greatly by supporting <table> tags which automatically generate the appropriate code.

<td> tags support the colspan and rowspan attributes if you want to create complex table structures.


        <td>First Name</td>
        <td><input type="text" name="firstName"/></td>
        <td>Last Name</td>
        <td><input type="text" name="lastName"/></td>
        <td>Home Country</td>
            <select name="countrySelect">
                <option>United States</option>
        <td colspan="2">Bio (Paragraph)</td>
        <td colspan="2">
            <textarea name="bio" rows="5" cols="30"></textarea>

Dynamically Building Tables

Sometimes you may want to build a table, but you don't want to manually include all of the cells inside the template. Perhaps you have a data source that you intend to iterate over in code to populate the cells of the table. In this case, you may just want to specify that the table has a particular number of rows and columns.

Use the rows and cols attributes of the <table> tag for this.


 <table rows="5" cols="10"></table.

You can also use a Java expression for these attributes.


 <table rows="board.getHeight()" cols="board.getWidth()"></table.

Images (the <img> tag)

CN1ML supports the <img> tag for including images in your UI. Currently it allows you to reference images from a resource file, or the class path. Use the src attribute to specify the image to use. Images in the resource file should be prefixed with res: and images on the classpath should be prefixed with jar:.


 <body layout="GridLayout(3,3)">
     <img src="res:bills.gif"/>
     <img src="res:bengals.gif"/>
     <img src="res:browns.gif"/>
     <img src="res:cardinals.gif"/>
     <img src="res:cowboys.gif"/>
     <img src="res:falcons.gif"/>
     <img src="res:panthers.gif"/>
     <img src="res:ravens.gif"/>
     <img src="res:bears.gif"/>

Note: In order to reference a resource file, you must pass at least one Resources object to the template's constructor. It doesn't matter what the key of this is. See ImagesExample for a complete example.

###Images in Labels and Buttons

<img> tags that are inside a <label> or <button> tag will be set as the parent Label or Button's icon. <img> tags occurring elsewhere inside the template will simply be wrapped in their own anonymous <label>.


You can place text anywhere inside the template. It the text is placed inside a <label>, <button>, or <textarea> tag, it will be set as the text of the resulting Label, Button, or TextArea. Text occurring elsewhere in the template will be automatically wrapped in an anonymous Label.

##Lists and Combo Boxes

Use the <select> tag to add a list or Combo Box to the UI. By default a <select> will be converted to a ComboBox, but if you add the size attribute with a number greater than 1, it will convert to a List.


<!-- Convert to combo box-->
   <option>Opt 1</option>
   <option>Opt 2</option>
   <option>Opt 3</option>

<!-- Convert to List -->
<select size="5">
   <option>Opt 1</option>
   <option>Opt 2</option>
   <option>Opt 3</option>

Dynamic Lists

You can provide the list with dynamic data for its model using the data attribute. If the data is a ListModel object, then you can provide it using the model attribute.


 <!-- Provide data (a Collection or array) -->
 <select data="menuItems"></select>
 <!-- Provide a list model -->
 <select model="myModel"></select>

Both of these examples require that you pass the data to the template as an attribute.


You can add a Tabs component to your form in two ways:

  1. Using the <tabs> element.
  2. Setting the class attribute of a any element to "Tabs".

Child elements of the Tabs component are then added as individual tabs. E.g.

  <div title="Tab 1">
    Hello World
  <div title="Tab 2" icon="res:customIcon.png">
  	Good bye World

Notice that child elements must include the title attribute, as it will be used as the tab label. They may optionally include an icon attribute as well to specify the icon that should be used for the tab. The syntax for the icon attribute value is the same as for the src attribute of the img tag.

See also Tabs Example


<script> tags are very useful for adding custom Java code to initialize or decorate your UI inline. There are three "special" variables that can be used inside of a script:

  1. self - Refers to the parent element of the script.
  2. parent - Refers to the grandparent element of the script (i.e. the parent of self).
  3. this - Refers to the CN1ML template instance.

E.g. Setting the tab placement to "top" using a script tag. Eventually I'd like to add support for most options like this directly in HTML attributes, but in cases where you need to customize a component, you can always use a script tag.

  <div title="Tab 1">
  	Hello World
  <div title="Tab 2">
    Tab 2 Content


CN1ML templates support localization via the i18n and i18n:xxx HTML attributes. To localize the text content of an element, use the i18n attribute. E.g.

<label i18n="welcome">Welcome</label>

To localize an attribute, you can should use the i18n:xxx attribute, where xxx is the attribute name that you wish to localize. E.g.

   <div title="Tab 1" i18n:title="tab1"> ...</div>

See also Localization Example.

For a brief introduction to localization in Codename One see this How to video.

##Setter Attributes

Since 0.1.6

For components with properties that follow the Java beans convention for setters (i.e. setXXX(value)), you can use HTML attributes of the form set:XXX="value".

E.g. To set the URL property of a WebBrowser you could use the set:URL attribute of the tag as follows:

 <div class="com.codename1.components.WebBrowser" 

Values of such attributes are interpreted the same way that data-XXX (i.e. Client Properties) attributes are parsed. Values are assumed to be strings, unless they are prefixed with java:.

E.g. For setting the browserNavigationCallback property of the WebBrowser, we need to pass an actual Java object that implements the BrowserNavigationCallback interface. To do this, we might pass the object to the template when we initialize it:

HashMap context = new HashMap();
context.put("browserCallback", myBrowserNavCallback);
MyTemplate tpl = new MyTemplate(context);
// ... etc...

And inside the MyTemplate.cn1ml file, you could reference the browserCallback object as follows:

<body attributes="BrowserNavigationCallback browserCallback;">
 <div class="com.codename1.components.WebBrowser" 

###Alternate 'dashed' syntax

You can also use an alternate 'dashed' syntax for your set: attributes. E.g.


When the CN1ML parser generates the Java source, it will automatically convert this to Camel Case (i.e. capitalize the first letter of each word).