corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Visual Mobile Designer Custom Components: Using Table Items in Java ME Applications

The Visual Mobile Designer (VMD) enables you to design Java ME applications using drag and drop components. With it, you can determine the application flow as well as the look and feel of your UI by using components supplied by the IDE, or by using components you design yourself. The VMD contains many standard components you can use to create your application: Lists, Alerts, Forms, Images, and more. It also includes custom components that simplify the creation of more complex components, such as wait screens, splash screens and login screens.

The TableItem component enables you to quickly create tables that consist of one or more columns, each with a header area and a data area that repeats for each record. The table can be larger than the screen in both vertically and horizontally. Users can use a cursor to scroll in either directions. Using the TableItem component of the Visual Mobile Designer (VMD) simplifies the design and implementation of data tables in your Java ME application.

If you are new to mobile application development in NetBeans, you should start with the NetBeans Java ME MIDP Quick Start Guide before continuing.

Contents

Content on this page applies to NetBeans IDE 6.5 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.5 or later
Java Development Kit (JDK) Version 6

Introduction: How the TableItem Component Works

This example shows you how to use the TableItem and TableModel components in a mobile application. It demonstrates the basic features of the the components as well as how to use them to visualize and manipulate data in your Java ME application inside the SimpleTableModel component. The TableItem custom component discussed in this tutorial uses a table from the SimpelTableModel interface, displays the data and, even allows the user to edit the data.

In addition to the TableItem custom component we need to use two other MIDP components: TextBox and Form. To learn more about these and other custom components included in the VMD palette see the Visual Mobile Designer Palette Guide

Installing and Running the Sample Application

Before we begin, you might want to see final result of the tutorial. Take the following steps to install the tableitemexample sample application:

  1. Download TableItem.zip file.
  2. Unzip the file.
  3. In the IDE, choose File > Open Project and browse to the folder that contains the unzipped file.
  4. Open the Project window. It should look like the following:

    Project Window with Table Item example

  5. In the Projects window, right-click the project node and choose Run Project (or press F6 key). As the application runs, an emulator window opens and displays the application running in the default device emulator.
  6. In the Emulator window, click the button underneath "Launch." The emulator displays a table, as shown:

    DefaultColorPhone emulator displaying the sample table application

  7. Edit the table:
    • Move the cursor from cell to cell using the directional arrows on the Select button.
    • Click the button underneath "Edit Cell" to edit the contents of a cell.
  8. Click the button underneath "Exit" to close the application.

Creating a Table with the TableItem Custom Component

Now that you have seen the TableItem component in action, we can show you how to create this application. To create the application do the following:

  1. Create the MyTableExample project
  2. Add packages and a visual MIDlet to the MyTableExample project
  3. Add components to MyTableMIDlet
  4. Assign SimpleTableModel to a TableItem Component
  5. Edit the SimpleTableModel design
  6. Add Commands to the Form and TextBox Components
  7. Connect the Components to create an application flow
  8. Insert a Pre-action into the source code
  9. Run the Project

Creating the MyTableExample Project

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select Java ME. Under Projects, select Mobile Application and click Next.
  2. Enter MyTableExample in the Project Name field. Change the Project Location to a directory on your system. From now on we can refer to this directory as $PROJECTHOME.
  3. Uncheck the Create Hello MIDlet checkbox. Click Next.
  4. Leave the Sun Java Wireless Toolkit 2.5.2 as the selected Emulator Platform. Click Finish.

Adding Packages and a Visual MIDlet to the MyTableExample Project

  1. Choose the MyTableExample project in the Project Window, then choose File > New File (Ctrl-N). Under Categories, select Java. Under File Types, select Java Package. Click Next.
  2. Enter tableitemexample in the Package Name field. Click Finish.
  3. Choose the tableitemexample package in the Project window, then choose File > New File (Ctrl-N) . Under Categories, select MIDP. Under File Types, select Visual MIDlet. Click Next.
  4. Enter MyTableItemMidlet into MIDlet Name and MIDP Class Name fields. Click Finish.

Adding Components to MyTableItemMidlet

  1. Switch your Visual MIDlet to the Flow view. Drag the following Screen components from the Component Palette and drop them in the Flow Designer:
    • Form
    • TextBox
  2. Double-click the form1 component to switch to the Screen Designer Window.
  3. Drag the TableItem component from the Form Item section of the Component Palette and drop it into form1. The TableItem component looks like the following when it is added to the Screen Designer:

    Shows Screen Designer with TableItem on theDevice Screen

  4. Click on tableItem1 and, in the Properties Window, type TableItem Example into the Label property.
  5. Select textBox1 and change the Title of the component to "Edit".
  6. Right-click the Resources folder then choose Add > SimpleTableModel.

Assigning SimpleTableModel to a TableItem Component

  1. Select the tableItem1 component in the Screen view.
  2. In the Properties Window, click on the Table Model property and choose simpleTableModel1 from the the list. Now you can see a table grid inside the tableItem component.

    Shows Device Screen with SimpleTableModel grid inside the TableItem

Editing SimpleTableModel

  1. Double-click on the tableItem1 component (Screen Designer) to open the Table Model Editor Window.
  2. In the Table Editor Model Window, change the number of rows and columns to 4.
  3. Check the Use Headers option.
  4. Rename the table headers to "column 1", "column 2", "column 3" and "column 4".
  5. Click OK to finish.

    Shows the Table Model  editing diagram

Adding Commands to the Form and TextBox Components

  1. Open the Flow Window.
  2. Choose Exit Command from the Commands section of the Component Palette. Drag and drop it into Flow Window (form1 component). Repeat this action for Item Command.
  3. Choose itemCommand1[Command] in the Screen Designer. In the Properties Window, change the Label property value to "Edit Cell".

    Shows the Screen Designer with  Assigned commands

  4. Choose the Ok Command from the Commands section of the Component Palette and drag and drop it into the textBox1 component.

Connecting Components

  1. In the Flow design window, click on the Start Point on the Mobile Device and drag it to the form1 component. In the same manner, connect the components together as shown in the following graphic.

    Shows the Flow window with components conneted by command lines

Inserting a Pre-action into the Source Code

  1. Switch to the Source Window.
  2. Find commandAction method in the source code: Insert following code into pre-action section of the okCommand1 (right after: if (command == okCommand1) {, where it says //insert pre-action code here):
     	  /* This part of the code is responsible for 
           * saving values into the table model 
           */ 
          // Get the position of the cursor in the table
              int row = tableItem1.getSelectedCellRow();
              int column = tableItem1.getSelectedCellColumn();
          // Set new value to the table model 
          simpleTableModel1.setValue(column,row,textBox1.getString());
          // Repaint table model on the screen device
          simpleTableModel1.fireTableModelChanged(); 
         
  3. Insert following code into pre-action section of the itemCommand1 (right after: } else if (command == itemCommand1) {, where it says //insert pre-action code here):
     		/* This part of the code is responsible for
             * getting value from the table model
             */
             if (textBox1 != null) {      
            // Get position of the cursor in the table.      
             int row = tableItem1.getSelectedCellRow();      
             int column = tableItem1.getSelectedCellColumn() 
            // Get value from the table model  
               Object value = simpleTableModel1.getValue(column,row); 
            // Set textBox string value   
              if (value != null)
              textBox1.setString(value.toString());          
                 else  
              textBox1.setString("");
            } 
             

Running the Project

  1. Press <F6> to Run the main project.

    Alternatively you could select Run > Run Main Project.

To Learn More about the TableItem and SimpleTableModel Components

The NetBeans IDE provides API documentation—javadocs—for the TableItem and TableModel components, as well as other components you can use in the VMD. To read the javadocs for the TableItem and TableModel components:

  1. Choose Help > Javadoc References > NetBeans MIDP Components.
  2. Click org.NetBeans.microedition.lcdui to see links for the component information.

Additional NetBeans Java ME Custom Component Tutorials