Creating a Touch Enabled SVG UI for Java ME Devices

This tutorial shows you how to develop rich UI's for Java ME based touch screen devices using SVG UI widgets. The rapidly growing number of Java ME based touch screen devices like the Samsung Omnia and Instinct, Nokia 5800 Xpress Music or Blackbery Storm make developing for these devices required reading for any serious mobile application developer. The application created in this tutorial works on most devices that support the SVG API for J2ME (JSR226) since Rich SVG UI widgets are also supported by devices without touch screens.

Contents

Content on this page applies to NetBeans IDE 6.9 or later

Requirements

To complete this tutorial, you need the following software and resources:

Software or Resource Version Required
NetBeans IDE with Java ME Version 6.9 or later
Java Development Kit (JDK) Version 6 or 7
Contact Browser Example NetBeans project
SVG UI Snippets NetBeans plugin

To ensure that the Java ME features are enabled, we recommend performing a Custom Installation and unchecking the Features on Demand component during installation of the IDE.

Getting the Project Environment Set Up

In addition to the required software listed above you should have the following installed on your computer:

  • The ContactBrowser.zip file should be unzipped into your NetBeans project folder.
  • The SVG snippets plugin should be downloaded and installed into NetBeans:
    1. In the IDE go to Tools > Plugins. Click the Downloaded tab and click Add Plugins.
    2. Navigate to the local folder containing the svg-snippets.nbm file, select the file and click Install.
  • Click Install and the IDE will guide you through the plugin installation. After the plugin is installed, you must restart the IDE.
  • After restarting the IDE, go to Tools > Options. Click Miscellaneous in the Options dialog window then click the SVG tab.
  • In the SVG Rich Components Snippets section open the Snippets drop down menu and choose J1 HOL SVG Snippets. Click OK.

    Options dialog box

top

Creating the UI in the SVG Composer

We are going to build a basic SVG-based application UI. Though the application is quite simple, it will show you how to create a new Java ME application, how to import SVG images from the SVG Composer into a visual MIDlet and how to run the project on a MIDP device emulator.

Background Information

SVG: JSR-226 specifies the API for rendering scalable, two-dimensional vector graphics in XML. Instead of encoding the contents of each pixel like the GIF and JPG formats, vector graphics contain only the drawing instructions for each pixel. This gives vector images several advantages:

  • They are scalable, so the image quality remains the same on displays of different sizes and resolutions. You can also stretch, flip, or reverse an image without losing quality.
  • They support scripting and animations, so they are ideal for interactive, data-driven graphics.
  • They are compact. Vector-based images can be as much as ten times smaller than an identical image in GIF or JPEG format. This important advantage makes it possible to create graphically rich applications for handheld devices with limited resources.
  • They allow users to interact with image content.
  • They are searchable, allowing users to search for text within the image. For example, users can search a map for specific street names, or search a list of restaurant menus for their favorite dish.

A full-featured subset of SVG aimed toward mobile devices is called SVG-Tiny and JSR-226 adopts version 1.1 of the SVG-Tiny profile as the official file format for J2ME vector graphics.

Introduction to SVG Widgets and Their Function

Creating User Interfaces is made easier using the SVG Composer and SVG Form Rich UI Components in NetBeans.

Overview of the SVG UI snippets available in the SVG Composer palette:

  • SVG List - Allows developer to create a list of UI elements needed for the UI.
  • SVG Radio Button - In the SVG snippets we are using this allows user to select gender.
  • SVG Combo Box - UI component for drop down list menus.
  • SVG Text - A field where text such as names of people, product ID's, etc can be displayed.
  • SVG Button - Buttons are used for Next, Previous, OK and other UI functions.
  • SVG Spinner - Enables user to scroll through a variable using Up and Down button.
  • SVG Slider - Allows user adjust a variable feature. For example, from light to dark, or 0-100%
  • SVG Label - Lets User Label a text field or other UI component.

SVG UI snippets available in the SVG Composer palette

This was only a streamlined introduction to the SVG UI Rich Components. For more details, please see the in the Netbeans Visual Mobile Designer Wiki page.

Steps to Create the SVG UI

To get us started quickly we will begin by opening the prepared ContactBrowser project that we use as a foundation to create the project.

  1. Open the prepared ContactBrowser Java ME project in the NetBeans IDE.

    Overview of the ContactBrowser project

  2. Open the employeeList.svg image in the SVG Composer and drag and drop two SVG Buttons and one SVG List Form Component into the Preview canvas from the palette as shown below. We will rename the buttons later in the tutorial.

    Below is the completed form with the following components added:

    • 2 x Buttons
    • 1 x List

    Form with two buttons and a list

  3. In the Preview window click the text in the OK button in the upper left and drag it to the left as shown in the screenshot below. This will allow the word "Select" to display correctly on the UI button.

    Form with two buttons and a list

  4. Our SVG UI needs an additional SVG File, employeeForm.svg, so again we open the file, and add the buttons, text fields, labels and buttons from the palette onto the image in the SVG composer and rename them to create the UI. When finished, it should look something like the image below.
    • Added components
      • 3 x Buttons
      • 4 x Labels
      • 2 x Text Fields
      • 1 x Radio Button
      • 1 x Spinner

    SVG components added

  5. In the Preview window click the text of the OK button on the lower left and drag it to the left as shown in the screenshot below. This will allow the word "Previous" to display correctly on the UI button.

    Text in the OK button dragged to the left

Creating a MIDlet in Visual Mobile Designer

The SVG Rich UI components framework is split into two parts:

  • SVG Snippets are used to build the UI in the SVG Composer or other SVG editor
  • Java ME libraries that support the UI functionality

We have already covered the SVG Composer and snippets, so our next step is to look at the runtime libraries in the Visual Mobile Designer which help to merge the SVG images with the application logic in the Java ME source code.

  1. To create a new Visual MIDlet, select File > New File and then in New File dialog box under Categories choose MIDP and in File Types choose Visual MIDlet. The new file should be part of org.netbeans.lab.ui.svg package.

    New File dialog box

  2. Inside of the newly created MIDlet switch to the Flow view. Find the SVG Components section of the Palette then drag and drop two SVG Form components onto the design canvas of the Flow view and use the right-click contextual menu to rename them employeeList and employeeForm.

    Two SVG Forms added

  3. The next step is to connect the SVG images to the source code. To connect the SVG UI components to the source code, we just drag and drop the employeeList.svg file from the Project window onto the employeeList component and repeat this action for the employeeForm.svg by dragging it onto the employeeForm.

    SVG UI components connected to the source code

  4. Now we have to change the instance name of the svgButtons to correspond with the actions they perform. Note that this does not change the button UI, we will do that later. To change the instance name of the svgButtons right-click on the button and select Rename from the contextual menu. Type the action name you want for the svgButtons instance in the New Name field of the Rename Instance Name dialog.

    Note: Make sure that the new name corresponds to the UI button's function when changing the instance names of UI components.

    Instance names of svgButtons changed

  5. In next step we have to change the default SVG Labels for the buttons and text fields to match the information displayed by the ContactBrowser application. To do so switch to the Screen view and display employeeForm. Then select the Label item in the Screen view and edit the text properties for each label and button so that they are like the screenshot below.

    Text propterties for labels and buttons changed

  6. We also need to change the instance name of the SVG text fields. Right-click the First Name and choose Rename from the contextual menu and change the name to svgTextFieldFirstName. Repeat this step for the Last Name field and rename it svgTextFieldLastName.
  7. Now in the drop down menu of the IDE project window, switch from employeeForm to employeeList and rename the buttons as shown in the screenshot below. You can see the changes in the Text field of the Properties window.

    Buttons' text propterties changed

top

Using the Visual Mobile Designer to Create the Application Flow

Now we are ready to create the application flow using the Flow view of Visual Mobile Designer.

  1. Switch from the Screen to the Flow view in the IDE.
  2. Connect the components as they are shown below. Start by clicking Started on the Mobile Device element and drag the mouse to the employeeList element. Repeat this method to make the other component connections.

    Application flow created

In the Source view you can see the mechanism that binds the SVG XML UI Component snippets with Java ME runtime libraries. In the example below you can see how the SVG Button snippets are bound to Java ME objects. The SVG Component is recognized based on the XML snippet ID, in this example, it is button_0.

Running the Application

Before we add the application logic we should test the Contact Browser UI on the emulator.

In the menu choose Run Main Project (F6) and after the emulator launches you can see and test your UI. We have not implemented the application logic yet, but can verify that the UI displays as expected based on the work done in the Visual Mobile Designer.

top

Adding Application Logic to the SVG User Interface

Now we can use the Visual Mobile Designer to create the application logic for the UI we created in the previous step. We will use some additional components and methods and finish by running the application in the Java ME SDK 3.0 emulator.

Background Information

  • SVG ListModel: This defines the methods components like the one SVGList uses to get the value of each cell in a list and the calculate the number of cells needed to display it. Logically the model is a vector, indices vary from 0 to ListDataModel.getSize() - 1. Any change to the contents or length of the data model must be reported to all of the SVGListDataListeners. If you are familiar with Swing's JList model, the SVGListModel should be easy to understand.
  • Entry Point component: The Entry Point component allows us to represent a method in a designed class. The method contains code specified by an action assigned to it, the "Entry Called" pin. This method is very useful when it is is necessary to execute code when switching between application screens, or in our case, to show the SVGButton logic in the Flow view of a Visual Midlet.

Creating a Custom SVGList Model

In this step we are going to create our own implementation of SVGListModel. Our implemention of SVGListModel is an anonymous class inside of the getSvgList method.

  1. Use the Navigator (Ctrl+7 if it is not already displayed) to find the getSvgList element under the employeeList component by right-clicking and choosing Got To Source from contextual menu.
  2. Inside of the getSVGList method add the following code after svgList = new SVGList(getEmployeeList(), "list_0");:
    		    //Creating and adding new SVGListModel as anonymous class. Implementation of ListModel
                svgList.setModel(new SVGList.ListModel() {
                    //Getting contact data as Vector from class EmployeeData
                    Vector data = EmployeeData.getData();  
                                    
                    /**
                     * Method returns combination of strings: First Name + Last Name
                     * @return Employee name as String
                     */
                    public Object getElementAt(int index) {
                        //Getting First Name String
                        String firstName = ((Employee) data.elementAt(index)).getFirstName();
                        //Getting Last Name String
                        String lastName = ((Employee) data.elementAt(index)).getLastName();
                        
                        return firstName + " " + lastName;
                    }
                     /**
                     * Number of employees
                     * @return number of employees
                     */
                    public int getSize() {
                        return data.size();
                    }
                    //not used
                    public void addDataListener(DataListener listener) { }               
                    
                    //not used
                    public void removeDataListener(DataListener listener) { }                          
                });

Note: You can use the code completion feature in the IDE and type the code for yourself.

Next and Previous Buttons Logic

In this step we will add logic for the SVGButtons which are a part of the employeeForm. These buttons allow application users to browse through employee data. By clicking Next, the user can jump to the next Empoloyee Contact. By pressing the Previous button, the user should see the previous employee record. In this step we are going to use the Entry Point element which we described earlier.

  1. Switch to to the Flow view and add two Entry Point components from the Flow category of the Palette. Right-click on the components and rename them to nextMethod and previousMethod as shown below:

    SVGButtons flow created

  2. Now we have to add a global private integer property index to the Visual MIDlet. Let's switch to the Source view and add the following under the Generated Fields guarded block:
    private int index = 0;
  3. Now we must add logic to update the UI in the employeeForm. The method we use here updates the SVG UI elements in the employeeForm component. In the Source editor at the end of the class add the following method:

    Note: You can use the IDE's code completion feature to enter the code.

    	        //**Method is responsible for populating employee UI form with information about employees.
                private void updateEmployeeFormUI() {
                  //Getting currently selected employee
                  final Employee employee = (Employee) EmployeeData.getData().elementAt(index);
                  //Setting First Name
                  getSvgTextFieldFirstName().setText(employee.getFirstName());
                  //Setting Last Name
                  getSvgTextFieldLastName().setText(employee.getLastName());
                  //Setting Gender
                  if (employee.getGender()) {
                      getSvgRadioButton().setSelected(false);
                      getSvgRadioButton1().setSelected(true);
                  } else {
                      getSvgRadioButton().setSelected(true);
                      getSvgRadioButton1().setSelected(false);
                  }    
                  //Setting age
                  getSvgSpinner().getModel().setValue(new Integer(employee.getAge()));
                }  
    	  

    Now we can start adding logic to the nextMethod and previousMethod.

  4. Switch to the Flow view and right-click on the nextMethod Entry Point and choose the Go To Source option from the popup menu.
  5. In the Source editor, add the following code to the nextMethod() method before switchDisplayable(null, getEmployeeForm().getSvgCanvas());. This code displays the next record in the queue of the employee data records. It also invokes the UI to update the information displayed.
    // Move to the next Employee record
            if (index < EmployeeData.getData().size() - 1) {
                //Update index.
                index++;
            }
            //Update UI - employee form
            updateEmployeeFormUI();
            //Go back to employeeForm
    		
  6. Switch to the Flow view and right-click on the previousMethod Entry Point and choose the Go To Source option from the popup menu.
  7. In the Source editor, add the following code to the previousMethod() method before switchDisplayable(null, getEmployeeForm().getSvgCanvas()); to enable the data browsing functionality to work.
    // Move to the previous Employee record
            if (index > 0) {
                //Update index
                index--;
            }
            //Update UI
            updateEmployeeFormUI();
            //Go back to employeeForm
    	  

    Next we need to make sure that the UI will be updated when employeeForm and set global index based on a selection done in in the employee list.

  8. Switch to the Flow view and right-click the select button in the employeeList and choose Go To Source from the popup menu.
  9. In the Source editor, add the following code to the getSelect() method after public void actionPerformed(SVGComponent svgComponent) {:
     //Updating global index
                        index = getSvgList().getSelectionModel().getSelectedIndex();
                        //Updating UI after selection has been made
                        updateEmployeeFormUI();
                        //Switching to the employeeForm

top

Testing the Application in A Touch Screen Device Emulator

We are ready to test our application in the Java ME SDK 3.0 DefaultFxTouchPhone1 device emulator that comes bundled in the NetBeans IDE installer. In order to launch the application we need to edit the Project Properties.

  1. Right-click the Project node and choose Properties from the contextual menu. In the Project dialog under Category choose Platform then select Java Platform Micro Edition SDK 3.0 from the Emulator Platform drop down menu.
  2. For touch screen device emulation select DefaultFxTouchPhone1 from the Device drop down menu and CLDC-1.1 as Device Configuration as shown below and click OK.

    Emulator platform and device selected

  3. Go to Run > Run Project and the application launches in the emulator. You can verify that touch screen properties are enabled by placing the mouse pointer over the SVG Components. The mouse cursor can be moved around the screen to navigate the application menus as a user would do with a fingertip.
  4. In the emulator we can perform some basic UI tests such as using the Previous or Next button to verify that the UI elements are working properly.

    Application launched in the emulator

Summary

In this tutorial you learned how to create an SVG-based, touch screen enabled UI for Java ME devices using the NetBeans IDE.

top


See Also

top

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo