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
Contributor
Piya15
Posts: 45
Registered: ‎11-29-2011
My Device: Blackberry Torch 9800,Blackberry Curve 9300
Accepted Solution

how to draw table in blackberry?

Hi friends ,I am totally new to blackberry.Even this is my first post .

 

I want to draw one table of 3 X 3. I use GridFieldManager Class ,but i am not getting horizontal and vertical lines .I searched a lot .I used paint method of each field which i added to GridFieldManager  to draw border,but it not showing that which i want. I want table with horizontal and vertical lines,so that each cell is differnt.how to do it.please help.

 

just want table like this &  then each cell i want to add field.but basically this structure i want.

 

 

Please use plain text.
Trusted Contributor
Albert_Anks
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: how to draw table in blackberry?

hi Piya15,

Wel come To BB Support Forum..

Use TableLayoutManager to fulfil your Requirement..

http://supportforums.blackberry.com/t5/Java-Development/Create-a-rich-UI-layout-with-TableLayoutMana...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Please use plain text.
Trusted Contributor
Albert_Anks
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: how to draw table in blackberry?

hi piya15,

Check out this link for how to use tablelayoutmanager...

http://devblog.blackberry.com/2009/10/how-to-use-table-view-layout/
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Please use plain text.
Contributor
Piya15
Posts: 45
Registered: ‎11-29-2011
My Device: Blackberry Torch 9800,Blackberry Curve 9300

Re: how to draw table in blackberry?

but how to use that class in application .i am not understanding.please clear it.
Please use plain text.
Trusted Contributor
Albert_Anks
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: how to draw table in blackberry?

Just put that class in your application package.

and to call that class for three column and three row like below,

 

TableLayoutManager colFMgr = new TableLayoutManager(new int[]
{
TableLayoutManager.USE_PREFERRED_SIZE,
TableLayoutManager.USE_PREFERRED_SIZE,
TableLayoutManager.USE_PREFERRED_SIZE,
}, Manager.HORIZONTAL_SCROLL);

 add this colFMgr in your screen with add(colFMgr); 

 

As you are new in blackberry development i have recommend you to read the documentation perfectly...

as i suggest you the link, that is provide all the things to use this manager..

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

Please use plain text.
Developer
jitendrasharma
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: how to draw table in blackberry?

Please find the code below to draw lines in grid:

 

package mypackage;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.Menu;

public class LinedGridFieldManager extends Manager {
	private int[] columnWidths;
	private int columns;
	private int allRowHeight = -1;
	int rowHeight=-1;
	/**
	 * Constructs a new GridFieldManager with the specified number of columns.
	 * Rows will be added as needed to display fields.
	 * Fields will be added to the grid in the order they are added to this manager,
	 * filling up each row left-to-right:
	 *
	 * For example a 2 column manager:
	 *
	 * [Field1][Field2]
	 * [Field3][Field4]
	 * [Field5]
	 *
	 * There are two constructors available: 
	 * In the first, 
	 * Column widths are all equal, and the manager will attempt to use all available width.
	 * The height of each row will be equal to the height of the tallest Field in that row.
	 * Field positional styles are respected, so fields that are smaller than the row/column
	 * they are in can be positioned left, right, top bottom, or centered.  They default to top left.
	 *
	 * @param columns Number of columns in the grid
	 * @param style
	 * 
	 * 
	 * @author modified 2010 by Arjun Dhar<br />
	 * @author modified 2009 by TBC Software http://www.vocshop.com
	 * @author Copyright 2008 by Anthony Rizk - http://www.thinkingblackberry.com
	 */
	public LinedGridFieldManager ( int columns, long style ) {
		super ( style );
		this.columns = columns;
	}
	/*
	 * In the second constructor, instead of passing a single int with the number  of columns, 
	 * an array of ints is passed with the width (in pixels) of each column.  The grid will be 
	 * constructed with as many columns as required to comply with the array, and the total width 
	 * of the grid __MAY__ be wider than the screen.   If so, the grid needs to be contained within 
	 * a horizontally scrolling screen or manager.
	 */

	public LinedGridFieldManager ( int[] columnWidths, long style ) {
		super ( style );
		this.columnWidths = columnWidths;
		this.columns = columnWidths.length;
	}

	public LinedGridFieldManager ( int[] columnWidths, int rowHeight, long style ) {
		this ( columnWidths, style );
		this.allRowHeight  = rowHeight;
	}


	//jk Override to eliminate menu choices
	//by default, we preffer to explicitly create menus rather than let the default 
	// blackberry menus come through.  Change this as you will.
	public void makeMenu ( Menu menu, int instance ) {
	}

	protected boolean navigationMovement ( int dx, int dy, int status, int time ) {

		int focusIndex = getFieldWithFocusIndex();

		/*
		 * in each while, after we have moved into the new field, update the display at top of screen
		 * right side of a row wraps to that row's left.  Top wraps to the bottom,  bottom wraps to the 
		 * top.  The default blackberry movement of "spiral down" using the right arrow or trackwheel 
		 * is overridden. */

		int lnFieldCount = getFieldCount();
		while ( dy > 0 ) {
			focusIndex += columns;

			if ( focusIndex >= lnFieldCount ) {
				if (lnFieldCount > 0) {
					//Wrap around to top row
					getField(lnFieldCount-1).setFocus();
					return true;
				} else {
					return false; // Focus moves out of this manager
				}
			} else {
				Field f = getField ( focusIndex );

				if ( f.isFocusable() ) { // Only move the focus onto focusable fields
					f.setFocus();
					dy--;
					//UpdateDisplay();
				}
			}
		}

		while ( dy < 0 ) {
			focusIndex -= columns;

			if ( focusIndex < 0 ) {
				//Wrap around to bottom row
				getField(0).setFocus();
				return true;
			} else {
				Field f = getField ( focusIndex );

				if ( f.isFocusable() ) {
					f.setFocus();
					dy++;
					//UpdateDisplay();
				}
			}
		}

		while ( dx > 0 ) {
			if (focusIndex == lnFieldCount-1) {
				return false;
			}
			if ((focusIndex+1)%columns == 0) {
				//Wrap around to beginning of line or boundary
				//focusIndex = (focusIndex-columns) + 1;
				return false;
			} else {
				focusIndex ++;
			}

			if ( focusIndex >= lnFieldCount ) {
				if (lnFieldCount > 0) {
					getField(1).setFocus();
					return true;
				} else {
					return false; // Focus moves out of this manager
				}
			} else {
				Field f = getField ( focusIndex );

				if ( f.isFocusable() ) {
					f.setFocus();
					dx--;
					// UpdateDisplay();
				}
			}
		}

		while ( dx < 0 ) {
			if (focusIndex%columns == 0) {
				//Wrap around to end of line Or Boundary
				//focusIndex = (focusIndex+columns) -1;
				return false;
			} else {
				focusIndex --;
			}


			if ( focusIndex < 0 ) {
				return false;
			} else {
				Field f = getField ( focusIndex );

				if ( f.isFocusable() ) {
					f.setFocus();
					dx++;
					//   UpdateDisplay();
				}
			}
		}

		return true;
	}

	protected void sublayout ( int width, int height ) {
		int y = 0;

		if ( columnWidths == null ) {
			columnWidths = new int[columns];

			for ( int i = 0; i < columns; i++ ) {
				columnWidths[i] = width / columns;
			}
		}

		Field[] fields = new Field[columnWidths.length];
		int currentColumn = 0;
		int rowHeight = 0;

		for ( int i = 0; i < getFieldCount(); i++ ) {
			fields[currentColumn] = getField ( i );
			layoutChild ( fields[currentColumn], columnWidths[currentColumn], height - y );

			if ( fields[currentColumn].getHeight() > rowHeight ) {
				rowHeight = fields[currentColumn].getHeight();
			}

			currentColumn++;

			if ( currentColumn == columnWidths.length || i == getFieldCount() - 1 ) {
				int x = 0;

				if ( this.allRowHeight >= 0 ) {
					rowHeight = this.allRowHeight;
				}
				
				

				for ( int c = 0; c < currentColumn; c++ ) {
					long fieldStyle = fields[c].getStyle();
					int fieldXOffset = 0;
					long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK;

					if ( fieldHalign == Field.FIELD_RIGHT ) {
						fieldXOffset = columnWidths[c] - fields[c].getWidth();
					} else if ( fieldHalign == Field.FIELD_HCENTER ) {
						fieldXOffset = ( columnWidths[c] - fields[c].getWidth() ) / 2;
					}

					int fieldYOffset = 0;
					long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;

					if ( fieldValign == Field.FIELD_BOTTOM ) {
						fieldYOffset = rowHeight - fields[c].getHeight();
					} else if ( fieldValign == Field.FIELD_VCENTER ) {
						fieldYOffset = ( rowHeight - fields[c].getHeight() ) / 2;
					}

					setPositionChild ( fields[c], x + fieldXOffset, y + fieldYOffset );
					x += columnWidths[c];
				}

				currentColumn = 0;
				y += rowHeight;
			}

			if ( y >= height ) {
				break;
			}
		}

		int totalWidth = 0;

		for ( int i = 0; i < columnWidths.length; i++ ) {
			totalWidth += columnWidths[i];
		}

		this.rowHeight=allRowHeight;
		if(this.rowHeight<=0)
			this.rowHeight=rowHeight;
		setExtent ( totalWidth, Math.min ( y, height ) );
	}
	
	protected void paint(Graphics graphics) {
		// TODO Auto-generated method stub
		super.paint(graphics);
		graphics.setColor(Color.BLACK);
		int y=0;
		for(int i=0;i<getFieldCount();i++)
		{
			int x=0;
			for(int j=0;j<columnWidths.length;j++)
			{
				graphics.drawLine(x-1, 0, x-1, getHeight());
				x+=columnWidths[j];
			}
			y+=rowHeight;
			graphics.drawLine(0, y-1, getWidth(), y-1);
			
		}
	}

}

 

Please use plain text.
Developer
jitendrasharma
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: how to draw table in blackberry?

Please find the project, I have created for testing.

Please use plain text.
New Contributor
NanuJogi
Posts: 2
Registered: ‎12-20-2011
My Device: 9300 Curve ver 9300/6.0.0.526
My Carrier: Vodaphone

Re: how to draw table in blackberry?

Hi

Big Thank you to jitendrasharma > Arjun Dhar >TBC Software ->Anthony Rizk for writing extremely good books on Blackberry.

 

Thanks jitendrasharma for providing project file for testing. It works pretty well :Clap:

 

Regards, Nanu Jogi

Please use plain text.
Contributor
Piya15
Posts: 45
Registered: ‎11-29-2011
My Device: Blackberry Torch 9800,Blackberry Curve 9300

Re: how to draw table in blackberry?

Yeah Hurrey this is what i am expecting .Thanks a lot a lot Jitendra :smileyvery-happy:
Please use plain text.
New Contributor
rahmatdinur
Posts: 4
Registered: ‎07-24-2012
My Device: curve 3g
My Carrier: indosat

Re: how to draw table in blackberry?

are you sure this script is working?, can you give me step by step to running this script, i'm new for developer blackberry and dont understand with java

Please use plain text.