How to show Table layout data in a MIDP Form
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 Example
Article
Contents |
Overview
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.
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(
tableData,
tableHeaders
);
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: Media:NetBeansTableItemMIDlet.zip



25 Sep
2009
Java ME does not provide a default table UI component. In order to address this limitation, the designers of the NetBeans mobility pack provide us with their own custom component for displaying tabular data, the TableItem. TableItems can be added to a Form using the Visual Form Designer, or can be created programatically. This article gives a nice overview of how to make use of the TableItem class provided by NetBeans. The article gives both a code example and a screenshot of the TableItem from the standard wireless toolkit emulator.
As the code example shows, the TableItem UI control uses a Model-View-Designer approach similar to that used for Swing controls in Java SE. The programmer needs to implement the TableModel interface when specifying the data to be stored in a TableItem. Alternatively, a SimpleTableModel class is provided which is a basic implementation of TableModel and allows users to simply pass an array containing the table data to its constructor.
While the TableItem UI control is useful, it is not perfect. From personal experience, I have found that the control tends to render slightly differently on different devices, and some experimentation is generally necessary to get the control to look exactly as you want it to. The following article demonstrates how to create more flexibile tables in Java ME which allow multi-line cell contents in a table: Create more flexible table in Java ME
Infomedica - Important warning!
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.infomedica 22:35, 17 July 2011 (EEST)