Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to show Table layout data in a MIDP Form

From Wiki
Jump to: navigation, search

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (11 Jul 2012)
There is a warning from Infomedica in comments (reproduced below) which shows that this layout has problems on some devices. This needs to be checked and this article either updated with a warning or a workaround.
The comment from Infomedica was "It should be noted that as of July 2011 the current version of netbeans's TableItem control has some serious difficulties in order to be properly rendered on S60 series. There is a number of posts on forums that can be googled - but no solution yet... The problems concerns in majority larger tables which have height grater than the screen. At least on Nokias E52 it does not scroll down till the end of the table."

Article Metadata
Code ExampleArticle
Created: firt (15 Apr 2007)
Last edited: hamishwillee (09 Aug 2013)



MIDP 2.0 CustomItem class allows developers to create personalized items to be used inside a Form. The layout and graphical appearance of these Items have to be developed by using low-level methods (as for Canvas displayables), and can be compiled an distributed as controls.

NetBeans Mobility Pack includes a new CustomItem that you can add to your project and allows you to show, in a form, a table layout information, like a DataGrid or a spreadsheet. This item is included in the org.NetBeans.microedition.lcdui package and can be created in the Visual Form Designer available at the IDE.

This article explains how to use TableItem class to create items with tabular layouts.

JavaME NetBeansTableItems.png

Create TableItems with NetBeans Visual Mobile Designer

If you want to visually create TableItems by using the Mobile Designer provided by NetBeans, you can look at the detailed tutorial available on NetBeans website: [1]

Create TableItems programmatically

TableItem instances use an underlying TableModel object to get the data to use inside the table. So, before creating a new TableItem, you have to create a TableModel to actually pass the required data to the new table.

Implementing TableModel

TableModel is an interface defining various methods that allows a TableItem to correctly retrieve this data. These methods are:

  • getColumnCount(): to retrieve the number of columns
  • getRowCount(): to retrieve the number of rows
  • getColumnName(int i): to retrieve the name of each column
  • getValue(int i, int j): to retrieve the value of a single table cell
  • isUsingHeaders(): to check if headers must be used or not
  • addTableModelListener(TableModelListener listener): to add a listener that will be notified of data changes
  • removeTableModelListener(): to remove a previously added listener

An example of class implementing TableModel is the following one:

class ExtendedTableModel implements TableModel
public int getColumnCount() {
return 3;
public String getColumnName(int i) {
return "Column " + i;
public int getRowCount() {
return 3;
public void addTableModelListener(TableModelListener arg0) {
public Object getValue(int i, int j) {
return "Cell " + i + ", " + j;
public boolean isUsingHeaders() {
return true;
public void removeTableModelListener(TableModelListener arg0) {

Note that the above code does not care about TableModelListeners, but you should definitely care if you're going to use data that dynamically changes. The following code shows how to use the ExtendedTableModel class to create a TableItem:

TableItem table = new TableItem(Display.getDisplay(this), "Complex Table", new ExtendedTableModel());

The SimpleTableModel

NetBeans also provides a class that already implements TableModel: SimpleTableModel. You can easily build a SimpleTableModel instances by passing a String[][] array containing the table data, and another String[] array containing the header labels.

Here's an example of how to create a SimpleTableModel instance, and how to use it to build a TableItem:

String[][] tableData = {
{"Cell 0,0", "Cell 0,1"},
{"Cell 1,0", "Cell 1,1"},
{"Cell 2,0", "Cell 2,1"}
String[] tableHeaders = {"Header 0", "Header 1"};
SimpleTableModel model = new SimpleTableModel(
TableItem table = new TableItem(Display.getDisplay(this), "My Table", model);

Source code download

You can download a sample MIDlet containing the code of this article here:

This page was last modified on 9 August 2013, at 06:16.
214 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.