Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Using TableModel, TableView, TableController and DataTemplate to Create a Selectable Table

by BlackBerry Development Advisor on ‎10-21-2010 01:33 PM - edited on ‎09-22-2011 04:47 PM by Retired (13,712 Views)



This article applies to the following:

  • BlackBerry® devices



The TableModel, TableView, TableController and DataTemplate classes can be used to create a table that captures feedback from a user.  You can detect when a user clicks on a specific row, column or cell.  The following example demonstrates this technique using TableController.FIELD_FOCUS.  An Eclipse® workspace is available for download using the link at the bottom of this article.


Screenshot of Sample Application




Sample Code


import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.table.*;

public class Tables extends UiApplication 
    private LabelField _focusLabel;
    private TableView theView;
    public static void main(String[] args) 
        UiApplication instance = new Tables();
    public Tables() 
        MainScreen theScreen = new MainScreen(Manager.NO_VERTICAL_SCROLL);
        //Initialize the TableModel, TableView and TableController.
        TableModel theModel = new TableModel();
        theView = new TableView(theModel);
        TableController theController = new TableController(theModel, theView, TableController.FIELD_FOCUS);
        //Initialize the DataTemplate.
        TestDataTemplate theTemplate = new TestDataTemplate(theView, 1, 4);
        theTemplate.createRegion(new XYRect(0,0,1,1));
        theTemplate.createRegion(new XYRect(1,0,1,1));
        theTemplate.createRegion(new XYRect(2,0,1,1));
        theTemplate.createRegion(new XYRect(3,0,1,1));
        theTemplate.setRowProperties(0, new TemplateRowProperties(60));
        theTemplate.setColumnProperties(0, new TemplateColumnProperties(80));
        theTemplate.setColumnProperties(1, new TemplateColumnProperties(80));
        theTemplate.setColumnProperties(2, new TemplateColumnProperties(80));
        theTemplate.setColumnProperties(3, new TemplateColumnProperties(80));
        //Add the Strings to the table.
        theModel.addRow(new String[]{"the","quick","brown","fox"});
        theModel.addRow(new String[]{"jumps","over","the","lazy"});
        theModel.addRow(new String[]{"dog","the","quick","brown"});
        //Add the table to the screen.

        //Initialize and add the focus LabelField to the screen.
        _focusLabel = new LabelField("");
        //Push the screen onto the display stack.

    //A Runnable that displays the label of the Button that was pressed.
    public class ShowIndexRunnable implements Runnable 
        public void run() 
            String focus = "Field with Focus: " + ((ButtonField)theView.getLeafFieldWithFocus()).getLabel();

    //Custom DataTemplate that creates a ButtonField for every String added to the TableModel.
    public class TestDataTemplate extends DataTemplate 
        public TestDataTemplate(DataView view, int rows, int columns) 
            super(view, rows, columns);
        public Field[] getDataFields(int modelRowIndex) 
            TableModel theModel = (TableModel) getView().getModel();
            //Get the data for the row.
            Object[] data = (Object[]) theModel.getRow(modelRowIndex);
            //Create a array to hold all fields.
            Field[] theDataFields = new Field[data.length];
            //Create a ButtonField with each String and set its Runnable to the ShowIndexRunnable class.
            for(int i=0; i < data.length; i++) 
                theDataFields[i] = new ButtonField((String) data[i], Field.USE_ALL_WIDTH | 
                		DrawStyle.ELLIPSIS | Field.FOCUSABLE | ButtonField.CONSUME_CLICK);
                ((ButtonField)theDataFields[i]).setRunnable(new ShowIndexRunnable());

            return theDataFields;




by Developer on ‎03-17-2011 10:41 AM

How could I style header row different from the rest of the rows?

by Developer on ‎09-22-2011 09:48 AM

Looks like code does work.

I mean, changing the focus or even clicking on the buttons do not change  '_focusLabel'

which I initialized with some text to make it visible.

See pic:





by BlackBerry Development Advisor on ‎09-22-2011 04:41 PM

The sample has stopped working in 7.0.  The ButtonField is missing the ButtonField.CONSUME_CLICK style.  I'll fix this right now.

Users Online
Currently online: 14 members 1,137 guests
Please welcome our newest community members: