01-02-2012 12:35 AM
How to add multiple rows on click of button .now it is just adding single row .plz give me idea how to do it ...waiting for reply
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
ObjectChoiceField obj1,obj2,obj3,obj4;
String chart[]={"101","102"};
String shade[]={"Shade1","Shade2"};
String rolls[]={"101","102"};
String cuts[]={"100-150","150-200"};
String selectedindex1,selectedindex2,selectedindex3,selec
LabelField lbl1,lbl2, lbl3, lbl4;
LinedGridFieldManager gfm;
HorizontalFieldManager hfm,hfm1,hfm2,hfm3;
VerticalFieldManager vfmMain;
public MyScreen()
{
// Set the displayed title of the screen
hfm1=new HorizontalFieldManager(HorizontalFieldManager.NO_V
hfm2=new HorizontalFieldManager(HorizontalFieldManager.NO_V
hfm3=new HorizontalFieldManager(HorizontalFieldManager.NO_V
hfm=new HorizontalFieldManager(HorizontalFieldManager.FIEL
vfmMain=new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL|Ma
obj1=new ObjectChoiceField("", chart,0,FIELD_LEFT);
obj2=new ObjectChoiceField("", shade,0,FIELD_LEFT);
obj3=new ObjectChoiceField("", rolls,0,FIELD_LEFT);
obj4=new ObjectChoiceField("", cuts,0,FIELD_LEFT);
lbl1=new LabelField("");
lbl2=new LabelField("");
lbl3=new LabelField("");
lbl4=new LabelField("");
ButtonField btnAdd=new ButtonField("ADD",FIELD_RIGHT);
gfm=new LinedGridFieldManager(4, LinedGridFieldManager.VERTICAL_SCROLL);
hfm1.setMargin(20, 0, 10, 0);
hfm1.add(new LabelField("Chart"));
hfm1.add(obj1);
hfm1.add(new LabelField("Shade"));
hfm1.add(obj2);
hfm2.add(new LabelField("Rolls"));
hfm2.add(obj3);
hfm2.add(new LabelField("Cuts"));
hfm2.add(obj4);
hfm.add(btnAdd);
gfm.add(new LabelField("Chart"));
gfm.add(new LabelField("Shade"));
gfm.add(new LabelField("Rolls"));
gfm.add(new LabelField("Cuts"));
vfmMain.add(hfm1);
vfmMain.add(hfm2);
vfmMain.add(hfm3);
vfmMain.add(hfm);
vfmMain.add(new SeparatorField());
vfmMain.add(gfm);
add(vfmMain);
btnAdd.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
selectedindex1 =chart[obj1.getSelectedIndex()];
selectedindex2 =shade[obj2.getSelectedIndex()];
selectedindex3 =rolls[obj3.getSelectedIndex()];
selectedindex4 =cuts[obj4.getSelectedIndex()];
while(lbl1.getText().equals("")||lbl2.getText().e
{
lbl1.setText(selectedindex1);
lbl2.setText(selectedindex2);
lbl3.setText(selectedindex3);
lbl4.setText(selectedindex4);
gfm.add(lbl1);
gfm.add(lbl2);
gfm.add(lbl3);
gfm.add(lbl4);
}
}
});
}
}
Solved! Go to Solution.
01-02-2012 01:34 AM
Plz check application by debugging, you must getting some exception.
01-02-2012 01:57 AM
01-02-2012 02:16 AM
Hi Piya,
I think the problem is LinedGridFieldManager, can u tell me in LinedGridFieldManager you r passing 4 as an argument wat is that 4 indicates....
Thanks,
Lakshman
01-02-2012 02:30 AM
01-02-2012 02:33 AM
This is that class which i used,now help me
public class LinedGridFieldManager extends Manager {
private int[] columnWidths;
private int columns;
private int[][] column;
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;
}
public LinedGridFieldManager ( int columns[][], long style ) {
super ( style );
this.column = 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);
}
}
}
01-02-2012 03:08 AM
Hi Piya,
Ok, Here u told that 4 indicates no of columns, so in your code total 4 fields adding to gfm (i.e LinedGridFieldManager) so that 4 fields comes under 1 row only if u try to give no of columns as 2 then u will get 2 rows...
Thanks,
Lakshman
01-02-2012 03:18 AM
01-02-2012 03:58 AM
Hi Piya,
I executed your code, and according to your logic it is working properly.
It is adding row only once because according to your logic only one row can be added to gfm, if you would want to add row on each click on button, please do the following:
selectedindex1 =chart[obj1.getSelectedIndex()];
selectedindex2 =shade[obj2.getSelectedIndex()];
selectedindex3 =rolls[obj3.getSelectedIndex()];
selectedindex4 =cuts[obj4.getSelectedIndex()];
LabelField lbl1=new LabelField("");
LabelField lbl2=new LabelField("");
LabelField lbl3=new LabelField("");
LabelField lbl4=new LabelField("");
if(lbl1.getText().equals("")||lbl2.getText().equals("")||lbl3.getText().equals("")||lbl4.getText().equals(""))
{
lbl1.setText(selectedindex1);
lbl2.setText(selectedindex2);
lbl3.setText(selectedindex3);
lbl4.setText(selectedindex4);
gfm.add(lbl1);
gfm.add(lbl2);
gfm.add(lbl3);
gfm.add(lbl4);
}
01-02-2012 03:59 AM
please find the screenshot I am attaching, what I am getting upon execution of your code.