Welcome!

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

Reply
Highlighted
New Contributor
Posts: 6
Registered: ‎02-04-2014
My Device: BlackBerry 9380
My Carrier: etislat

How to make Table View scroll Vertically as well as Horizontally in blackberry7?

I am developing an Blackberry Application using Java.I have to show many number of column Horizontally and Rows Vertically.I need to show grid of items like Excel sheet. I am using TableView. But the problem is that TableView does not support horizontal scroll. I am beginner on his platform. Please suggest me how can I achieve the same.

Following is my Code which I use for Horizontal scroll

 

import net.rim.device.api.system.Bitmap;import net.rim.device.api.system.Display;import net.rim.device.api.ui.Color;import net.rim.device.api.ui.Field;import net.rim.device.api.ui.Font;import net.rim.device.api.ui.Manager;import net.rim.device.api.ui.TouchEvent;import net.rim.device.api.ui.XYRect;import net.rim.device.api.ui.component.BitmapField;import net.rim.device.api.ui.component.LabelField;import net.rim.device.api.ui.component.table.DataTemplate;import net.rim.device.api.ui.component.table.TableController;import net.rim.device.api.ui.component.table.TableModel;import net.rim.device.api.ui.component.table.TableView;import net.rim.device.api.ui.component.table.TemplateColumnProperties;import net.rim.device.api.ui.component.table.TemplateRowProperties;import net.rim.device.api.ui.container.HorizontalFieldManager;import net.rim.device.api.ui.container.MainScreen;import net.rim.device.api.ui.container.VerticalFieldManager;import net.rim.device.api.ui.decor.BackgroundFactory;/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */publicfinalclassMyScreenextendsMainScreen{privateTableModel _tableModel;privateHorizontalFieldManager hfm;privateTableView _tableView;privateTableController _controller;/**
     * Creates a new MyScreen object
     */publicMyScreen(){super(Manager.NO_VERTICAL_SCROLL);         _tableModel =newTableModel();// Set up view and controller//         _tableView=new Ta          hfm=newHorizontalFieldManager(Manager.HORIZONTAL_SCROLL){protectedvoid sublayout(int maxWidth,int maxHeight){// TODO Auto-generated method stub//               Dialog.alert("Hallo");super.sublayout(maxWidth, maxHeight);}};         _tableView =newTableView(_tableModel);         hfm.add(_tableView);
         _tableView.setDataTemplateFocus(BackgroundFactory.createLinearGradientBackground(Color.WHITE,Color.WHITE,Color.GREENYELLOW,Color.GREEN));         _controller =newTableController(_tableModel, _tableView);         _tableView.setController(_controller);         _tableView.setMargin(0,10,10,10);
         setStyle();         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"}); 
         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         _tableModel.addRow(newObject[]{"Hello world","Hello world","Hello world","Hello world"});         add(hfm);}publicvoid setStyle(){// Specify a data template for each item describing a block with four rows and// two columns. Create a region so that the image will be displayed across// four rows.DataTemplate dataTemplate =newDataTemplate(_tableView,1,4){/**
             * @see DataTemplate#getDataFields(int)
             */publicField[] getDataFields(int modelRowIndex){Object[] data =(Object[]) _tableModel.getRow(modelRowIndex);Field[] fields =newField[data.length];for(int i =0; i < data.length; i++){if(data[i]instanceofBitmap){                        fields[i]=newBitmapField((Bitmap) data[i]);}elseif(data[i]instanceofString){VerticalFieldManager vm=newVerticalFieldManager();                    vm.add(newLabelField(data[i],USE_ALL_WIDTH));                        fields[i]= vm;}else{                        fields[i]=(Field) data[i];}}return fields;}};// Set the style and apply it to the data template via the setRowProperties() method        dataTemplate.createRegion(newXYRect(0,0,1,1),null);        dataTemplate.setRowProperties(0,newTemplateRowProperties(Font.getDefault().getHeight()+30));//        for(int i = 0; i < 3; i++)//        {            dataTemplate.createRegion(newXYRect(1,0,1,1),null);            dataTemplate.setRowProperties(0,newTemplateRowProperties(Font.getDefault().getHeight()+30));//        }            dataTemplate.createRegion(newXYRect(2,0,1,1),null);            dataTemplate.createRegion(newXYRect(3,0,1,1),null);            dataTemplate.setRowProperties(0,newTemplateRowProperties(Font.getDefault().getHeight()+30));            dataTemplate.setRowProperties(0,newTemplateRowProperties(Font.getDefault().getHeight()+30));// Calculate and programmatically set the width of the image section of the table        dataTemplate.setColumnProperties(0,newTemplateColumnProperties(Display.getWidth()/3));        dataTemplate.setColumnProperties(1,newTemplateColumnProperties(Display.getWidth()/3));        dataTemplate.setColumnProperties(2,newTemplateColumnProperties(Display.getWidth()/3));        dataTemplate.setColumnProperties(3,newTemplateColumnProperties(Display.getWidth()/3));// Set the width of the text portion of the table//        dataTemplate.setColumnProperties(1, new TemplateColumnProperties(Display.getWidth() - 100));//Apply the template to the view        _tableView.setDataTemplate(dataTemplate);        dataTemplate.useFixedHeight(true);}}

Any Kind of help will be high appreciated.

Thanks in Advance.

 
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

I am preparing a simpler example, with explanations, that should help with this. 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

I have created a simpler screen, see below.

 

It does show the same problem (it does not scroll horizontally) and I have tried to play round with it a lot,  I thought I would publish this simpler version to make it easier for other people to test.

 

To be blunt, my testing of this has convinced me that TableView is flakey, and not well documented. 

Some of the flakey things I found while testing:

a) It does not work at all on some SImulators - it wasn't until I tried this code on a OS 7.0 Simulator that I got it to actually display. 

b) This sample does scroll vertically, but only if you use RichTextFields and not LabelFields. 

c) It also will fail completely unexpectedly if you don't configure it correctly, see comments. 

 

My "not well documented" comments follow from (c) - it is not obvious to me from the documentation how to set this up so that it does work correctly. 

 

But I am very happy for someone with more experience of this to convince my that it is just me not reading or not understanding correctly.  But first, try to get this scrolling horizontally.....

 

public class TableViewTestScreen extends MainScreen {

	private TableModel _tableModel;
    private TableView _tableView;
    private TableController _controller;

    public TableViewTestScreen() {
    	// If you set this as vertical scrolling, the TableView will barf saying it has to be in a
    	// non scrolling Manager.  But it doesn't seem to care about horizontal scrolling
        super(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL);

        _tableModel = new TableModel();
         // Set up view and controller
         _tableView = new TableView(_tableModel);
         add(_tableView);
         _controller = new TableController(_tableModel, _tableView);
         _tableView.setController(_controller);
         setDataTemplate();
         // Populate
         for ( int i = 0; i < 20; i++ ) {
        	 String rowId = "Row " + Integer.toString(i+1);
        	 _tableModel.addRow(new Object[] {rowId + "A", rowId + "B",rowId + "C", rowId + "D"}); 
         }

    }

    public void setDataTemplate() {
        // Specify a data template for the View
        DataTemplate dataTemplate = new DataTemplate(_tableView, 1, 4) {
            public Field[] getDataFields(int modelRowIndex) {
                Object[] data = (Object[]) _tableModel.getRow(modelRowIndex);
                Field[] fields = new Field[data.length];
                for(int i = 0; i < data.length; i++) {
                    if(data[i] instanceof String) {
                    	// It will be in this case because every column is a String
                    	fields[i] = new RichTextField((String)data[i]);
                    	// if you change to a LabelField, then Vertical scrolling stops working
                    	// fields[i] = new LabelField((String)data[i]);
                    } else {
                    	throw new RuntimeException("Non supported column type found: " + data[i].getClass().getName());
                    }
                }
                return fields;
            }
        };  

        // There needs to be the same number of regions and the entries in the row data returned
        // by getDataFields, otherwise you get an IllegalStateException 
        dataTemplate.createRegion(new XYRect(0, 0, 1, 1), null);
        dataTemplate.createRegion(new XYRect(1, 0, 1, 1), null);
        dataTemplate.createRegion(new XYRect(2, 0, 1, 1), null);
        dataTemplate.createRegion(new XYRect(3, 0, 1, 1), null);

        // Remove following and you get a NullPointerException (in layout processing it appears)
        // But you can change this as much as you want, it has no effect unless you also have
        // useFixedHeight(true).  
        dataTemplate.setRowProperties(0, new TemplateRowProperties(Font.getDefault().getHeight() +
                 10));
        dataTemplate.useFixedHeight(true);
        
        // Calculate and set the width of each column, to require horizontal scrolling
        double colWidthDouble = Display.getWidth()/ 2.5;
        int colWidth = (int) colWidthDouble;
        // The idea of this is just to set the width to clearly show that horizontal scrolling does not work
        // You will see 3 columns and the 4th is not visible, and the scrolling does not 
        // show you all of the 3rd column either...
        dataTemplate.setColumnProperties(0, new TemplateColumnProperties(colWidth));
        dataTemplate.setColumnProperties(1, new TemplateColumnProperties(colWidth));
        dataTemplate.setColumnProperties(2, new TemplateColumnProperties(colWidth));
        dataTemplate.setColumnProperties(3, new TemplateColumnProperties(colWidth));

        _tableView.setDataTemplate(dataTemplate);

    }
}

 

New Contributor
Posts: 6
Registered: ‎02-04-2014
My Device: BlackBerry 9380
My Carrier: etislat

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

Can you suggest me alternate solution
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

[ Edited ]

The advantage with TableView is that it fits the MVC pattern.  if you can sacrifice that, then you could try TableLayoutManager. 

 

http://supportforums.blackberry.com/t5/Java-Development/Create-a-rich-UI-layout-with-TableLayoutMana...

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

The problem appears to be that the TableView does not correctly indicate focus region to the enclosing Manager, which then doesn't scroll there.

 

I've changed the start of the class to the following, and it appears the width and virtualWidth values I see are what I would expect. 

 

    public TableViewTestScreen() {
    	// If you set this as vertical scrolling, the TableView will barf saying it has to be in a
    	// non scrolling Manager.  But it doesn't seem to care about horizontal scrolling
        super(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL);

        _tableModel = new TableModel();
         // Set up view and controller
         _tableView = new TableView(_tableModel) {
        	 protected void sublayout(int maxWidth, int maxheight) {
        		 super.sublayout(maxWidth, maxheight);
        		 int width = this.getWidth();
        		 int virtualWidth = this.getVirtualWidth();
        		 System.out.println("TableView Width: " + Integer.toString(width) + ":" + Integer.toString(virtualWidth));
        	 }
         };
         HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR) {
        	 protected void sublayout(int maxWidth, int maxheight) {
        		 super.sublayout(maxWidth, maxheight);
        		 int width = this.getWidth();
        		 int virtualWidth = this.getVirtualWidth();
        		 System.out.println("HorizontalFieldManager Width: " + Integer.toString(width) + ":" + Integer.toString(virtualWidth));
        	 }
         };
         hfm.add(_tableView);
         add(hfm);
....

 

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

I wrote a sample spreadsheet view that scrolls both directions.  Have a look at the link below.  Does that provide what you're looking for?

 

Create a screen with stationary headings

 

Here's what it looks like:

 

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Contributor
Posts: 6
Registered: ‎02-04-2014
My Device: BlackBerry 9380
My Carrier: etislat

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

[ Edited ]

Hello 

 

Thanks for answer.It is very close to what i want but the problem is that the horizontal scrolling is infinite. So If i scroll horizontally it do not stop scrolling.Please give me solution as. I am not able to use any other code now.

 

Thanks

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

Have you tried TableLayoutManager?

New Contributor
Posts: 6
Registered: ‎02-04-2014
My Device: BlackBerry 9380
My Carrier: etislat

Re: How to make Table View scroll Vertically as well as Horizontally in blackberry7?

Hello @Peter 

 

Now Its too late to do experiment can you please give some guidence .or make a simple example with labelfield so that i can use it easily 

 

Thanks