07-03-2012 02:33 AM
package mypackage;
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 MainScreen
{
private LabelField _focusLabel;
private TableView theView;
public Tables()
{
super(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);
theView.setController(theController);
//Initialize the DataTemplate.
TestDataTemplate theTemplate = new TestDataTemplate(theView, 1, 1);
theTemplate.createRegion(new XYRect(0,0,1,1));
theTemplate.setRowProperties(0, new TemplateRowProperties(Display.getWidth()));
theTemplate.setColumnProperties(0, new TemplateColumnProperties(80));
theTemplate.useFixedHeight(true);
theView.setDataTemplate(theTemplate);
//Add the Strings to the table.
theModel.addRow(new String[]{"the"});
theModel.addRow(new String[]{"jumps"});
theModel.addRow(new String[]{"dog"});
//Add the table to the screen.
add(theView);
//Initialize and add the focus LabelField to the screen.
_focusLabel = new LabelField("");
add(_focusLabel);
//Push the screen onto the display stack.
//pushScreen(theScreen);
}
//A Runnable that displays the label of the Button that was pressed.
public class ShowIndexRunnable implements Runnable
{
ShowIndexRunnable()
{}
public void run()
{
theView.setFocus();
String focus = "Field with Focus: " + ((ButtonField)theView.getLeafFieldWithFocus()).get Label();
synchronized(Application.getEventLock())
{
_focusLabel.setText(focus);
}
}
}
//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[0] = new ButtonField((String) data[0], Field.USE_ALL_WIDTH |
DrawStyle.ELLIPSIS | Field.FOCUSABLE | ButtonField.CONSUME_CLICK);
((ButtonField)theDataFields[0]).setRunnable(new ShowIndexRunnable());
// }
return theDataFields;
}
}
}
I want to do it, one row with one column.However, onyl the first row with "the" button appears. whats the error?