Search code examples
blackberryjava-metableviewblackberry-eclipse-plugintablemodel

Insert data in TableModel.addRow into Blackberry TableView


I need to Create a table where 3 columns are needed and can have multiple rows. I am using BlackBerry API version 6. I have debugged my code and it's giving IllegalArgumentException. I am not able to sort this error out.

My code is as follows:

public class designTableLayout extends MainScreen{

    TableModel theModel = new TableModel();
    theView = new TableView(theModel);
    TableController theController = new TableController(theModel, theView,
                                                        TableController.FIELD_FOCUS);
    theView.setController(theController);

    HeaderTemplate theTemplate = new HeaderTemplate(theView, 1, 3);
    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.setRowProperties(0, new TemplateRowProperties(60));
    theTemplate.setColumnProperties(0, new TemplateColumnProperties(40));
    theTemplate.setColumnProperties(1, new TemplateColumnProperties(40));
    theTemplate.setColumnProperties(2, new TemplateColumnProperties(40));
    theTemplate.useFixedHeight(true);
    theView.setDataTemplate(theTemplate);

    theModel.addRow(new String[]{"the","quick","brown"});// problem arises here
    theModel.addRow(new String[]{"jumps","over","the"});
    theModel.addRow(new String[]{"dog","the","quick"});
    add(theView);
}

class HeaderTemplate extends DataTemplate {
    LabelField field1 = new LabelField("field1");
    LabelField field2 = new LabelField("field2");
    LabelField field3 = new LabelField("field3");

    public HeaderTemplate(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 = {field1, field2, field3};
         data = (Object[]) theModel.getRow(modelRowIndex);

         //Create a array to hold all fields.
         Field[] theDataFields = new Field[data.length];
         theDataFields[0] = new LabelField(field1/*, DrawStyle.ELLIPSIS*/);
         theDataFields[1] = new LabelField(field2/*, DrawStyle.ELLIPSIS*/);
         theDataFields[2] = new LabelField(field3/*, DrawStyle.ELLIPSIS*/);

         return theDataFields;
    }

}

Solution

  • I know you probably are using some of this code just to test your table model, but I think your template should look more like this:

    class HeaderTemplate extends DataTemplate {
    
        public HeaderTemplate(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];
             theDataFields[0] = new LabelField((String)data[0], DrawStyle.ELLIPSIS);
             theDataFields[1] = new LabelField((String)data[1], DrawStyle.ELLIPSIS);
             theDataFields[2] = new LabelField((String)data[2], DrawStyle.ELLIPSIS);
    
             return theDataFields;
        }
    
    }
    

    And then add your data as an Object[]:

    theModel.addRow(new Object[]{"the","quick","brown"});
    

    Here is the BlackBerry example on this