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
Regular Contributor
meghana_datar
Posts: 56
Registered: ‎11-24-2010

Re: How to implement custom llistfield having alternate color in row

[ Edited ]

hi You can draw one row in listFieldCallback method, and for columns you need to specify/give fixed column width depending upon no. of  columns and then draw everything you need. 

For e.g :

You have to draw row with 4 equal columns . Column width will be  Display.getWidth()/4. 

Or if you want to set different column with you can set it in and array and use this array while drawing text.

 

 

 

int columnWidth  = Display.getWidth()/4;

 

public void drawListRow(ListField listField,Graphics graphics,int index,int y,int width){

 

// setbackground color : to set alternate BG color for rows.

if(index%2  == 0  ){

graphics.setColor(); 

}else{

graphics.setColor(); 

}

graphics.fillRect(0,0,width,listField.getContentHeight());

 

int xpos = 0;

 

graphics.setFont(font);// please set a font value

// this is first column text

graphics.drawText("column1",xpos,y);

xpos +=  columnWidth;

 

graphics.drawText("column2",xpos,y);

 

xpos +=  columnWidth;

graphics.drawText("column3",xpos,y);

 

xpos +=  columnWidth;

 

graphics.drawText("column4",xpos,y);

 

 

}

 

if you want to set column width dynamically. Go for "for" loop

eg:

 

for(;:smileywink:{

graphics.drawText("Enter text here dyanically",xpos,ypos);

xpos +=  columnWidth[];

}

 

Hope this helps..

 

 

Please use plain text.
Developer
ravi1989
Posts: 773
Registered: ‎07-21-2012
My Carrier: 0

Re: How to implement custom llistfield having alternate color in row

Hello sir thanks for reply ...!! every thing is fine but i also need alternate color of row as i display in image , i used your code and modified it but didnot get it please please me?

public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
if(index%2 == 0 ){
graphics.setColor(Color.RED);
graphics.fillRect(0, 0, width, y);
}else{
graphics.setColor(Color.WHEAT);
graphics.fillRect(0,0,width,y);
}
//graphics.fillRect(0,0,width,y);

int xpos = 0;int ypos = 0;
graphics.setFont(Font.getDefault());// please set a font value
// this is first column text
graphics.drawText("column1",xpos,y);
xpos += columnWidth;

graphics.drawText("column2",xpos,y);

xpos += columnWidth;
graphics.drawText("column3",xpos,y);

xpos += columnWidth;

graphics.drawText("column4",xpos,y);

}
Please use plain text.
Regular Contributor
meghana_datar
Posts: 56
Registered: ‎11-24-2010

Re: How to implement custom llistfield having alternate color in row

[ Edited ]

public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {

if(index%2  == 0  ){
graphics.setColor(Color.BlACK); 
graphics.fillRect(0, y, width, listField.getRowHeight());
}else{
graphics.setColor(Color.WHITE); 
graphics.fillRect(0, y, width, listField.getRowHeight());
}

 

 

here rowwidth =  total display width you require to display .

rowheight =  font.getHeight();

 

you have used y where y implies to y position of draw. Please modify and check

Please use plain text.
Developer
ravi1989
Posts: 773
Registered: ‎07-21-2012
My Carrier: 0

Re: How to implement custom llistfield having alternate color in row

Hello sir used this code ..i get alternative color in row but focus is in not coming  on the row .please help me..

package com.black.applicationloader;

import java.util.Vector;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

import com.black.blackinterface.BlackInterface;
import com.black.common.BaseScreen;
import com.black.components.CustomListField;

public class BlackSecondScreen extends BaseScreen implements BlackInterface,ListFieldCallback {
	
	 private static final String[] _elements = {"First element", "Second element", "Third element", "Fourth element", "Fifth element"};
	 private Vector _listElements = new Vector(_elements.length, 1);
	 int columnWidth  = Display.getWidth()/4;
	
	BlackSecondScreen(){
		
		ListField colourList = new ListField();
		
		 colourList.setCallback(this);
		  int elementLength = _elements.length;
		 for(int count = 0; count < elementLength; ++count)
	        {
	            colourList.insert(count);
	            this.insert(_elements[count], count);
	        }
		 
		 add(colourList);

		
	}

	public void createComponents() {
		// TODO Auto-generated method stub
		
	}

	public void layoutComponents() {
		// TODO Auto-generated method stub
		
	}

	public void initializeListeners() {
		// TODO Auto-generated method stub
		
	}

	public void setComponentsXYMargins() {
		// TODO Auto-generated method stub
		
	}

	public void drawListRow(ListField listField, Graphics graphics, int index,
			int y, int width) {
		if(index%2  == 0  ){
			graphics.setColor(Color.PERU); 
			graphics.fillRect(0, 0, Display.getWidth(),getContentHeight());
			}else{
			graphics.setColor(Color.WHEAT); 
			graphics.fillRect(0, 0, Display.getWidth(),getContentHeight());
			}
			//graphics.fillRect(0,0,width,y);
			 
			int xpos = 0;int ypos = 0;
			graphics.setFont(Font.getDefault());// please set a font value
			// this is first column text
			graphics.setColor(Color.BLACK); 
			graphics.drawText("column1",xpos,y);
			xpos +=  columnWidth;
			 
			graphics.setColor(Color.RED); 
			graphics.drawText("column2",xpos,y);
			 
			xpos +=  columnWidth;
			graphics.drawText("column3",xpos,y);
			 
			xpos +=  columnWidth;
			 
			graphics.drawText("column4",xpos,y);
		
	}

	public Object get(ListField listField, int index) {
		// TODO Auto-generated method stub
		 return _listElements.elementAt(index);
	}

	public int getPreferredWidth(ListField listField) {
		// TODO Auto-generated method stub
		return Graphics.getScreenWidth();
	}

	public int indexOfList(ListField listField, String prefix, int start) {
		// TODO Auto-generated method stub
		return _listElements.indexOf(prefix, start);
	}
	
	public void insert(String toInsert, int index) 
    {
        _listElements.insertElementAt(toInsert, index);
    }
	
	 public void erase() 
	    {
	        _listElements.removeAllElements();
	    }
	

}

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,523
Registered: ‎07-14-2008

Re: How to implement custom llistfield having alternate color in row

I think you have not understand how drawListRow works.

 

The call provides you with a number of parameters.  the two most important ones are

y - this is the y position that you start all your painting

width - this is the width that you can paint to.

 

In addition, you should only paint one row - the height of the row is a function of the ListField

 

This has implications throughout your method, so for example, this  is wrong:

if(index%2  == 0  ){
graphics.setColor(Color.PERU);
graphics.fillRect(0, 0, Display.getWidth(),getContentHeight());
}else{
graphics.setColor(Color.WHEAT);
graphics.fillRect(0, 0, Display.getWidth(),getContentHeight());
}

 

You need to change it to:

if(index%2  == 0  ){
graphics.setColor(Color.PERU);
graphics.fillRect(0, y, width, listField.getRowHeight());
}else{
graphics.setColor(Color.WHEAT);
graphics.fillRect(0, y, width, listField.getRowHeight());
}

 

There are other implications in your code, specifically you initial setting of yPos. 

 

Please change your code to take account of this, repost your corrected drawListRow() method, and then we can talk about about focus indication. 

 

@meghana_datar - can I suggest you review your supplied code and correct it to match this too in case someone attempts to copy your code and it doesn't work. 

Please use plain text.
Regular Contributor
meghana_datar
Posts: 56
Registered: ‎11-24-2010

Re: How to implement custom llistfield having alternate color in row

[ Edited ]

The only change done is overridding the drawFocus function of ListField

 

package mypackage;


import java.util.Vector;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;

public class BlackSecondScreen extends MainScreen implements ListFieldCallback {

private static final String[] _elements = {"First element", "Second element", "Third element", "Fourth element", "Fifth element"};
private Vector _listElements = new Vector(_elements.length, 1);
int columnWidth = Display.getWidth()/4;

BlackSecondScreen(){
ListField colourList = new ListField(6,FOCUSABLE){

protected void drawFocus (Graphics graphics, boolean on)
{
XYRect rect = new XYRect();
graphics.setGlobalAlpha(150);
getFocusRect(rect);
drawHighlightRegion(graphics,HIGHLIGHT_FOCUS,true,rect.x,rect.y,rect.width,rect.height);
}
};

colourList.setCallback(this);
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
colourList.insert(count);
this.insert(_elements[count], count);
}

add(colourList);


}

public void createComponents() {
// TODO Auto-generated method stub

}

public void layoutComponents() {
// TODO Auto-generated method stub

}

public void initializeListeners() {
// TODO Auto-generated method stub

}

public void setComponentsXYMargins() {
// TODO Auto-generated method stub

}

public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
if(index%2  == 0  ){
graphics.setColor(Color.BlACK); 
graphics.fillRect(0, y, width, listField.getRowHeight());
}else{
graphics.setColor(Color.WHITE); 
graphics.fillRect(0, y, width, listField.getRowHeight());
}
//graphics.fillRect(0,0,width,y);

int xpos = 0;
graphics.setFont(Font.getDefault());// please set a font value
// this is first column text
graphics.setColor(Color.BLACK);
graphics.drawText("column1",xpos,y);
xpos += columnWidth;

graphics.setColor(Color.RED);
graphics.drawText("column2",xpos,y);

xpos += columnWidth;
graphics.drawText("column3",xpos,y);

xpos += columnWidth;

graphics.drawText("column4",xpos,y);

}

public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return _listElements.elementAt(index);
}

public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return Graphics.getScreenWidth();
}

public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return _listElements.indexOf(prefix, start);
}

public void insert(String toInsert, int index)
{
_listElements.insertElementAt(toInsert, index);
}

public void erase()
{
_listElements.removeAllElements();
}

}

Please use plain text.
Regular Contributor
meghana_datar
Posts: 56
Registered: ‎11-24-2010

Re: How to implement custom llistfield having alternate color in row

Hey Peter, Thanks.. You are right.. About the y and width.  and that is most imporatnat part of ListField drawRow methd.

Please use plain text.
Developer
peter_strange
Posts: 19,523
Registered: ‎07-14-2008

Re: How to implement custom llistfield having alternate color in row

This is still wrong:

 

int xpos = 0;int ypos = 0;

Please use plain text.
Regular Contributor
meghana_datar
Posts: 56
Registered: ‎11-24-2010

Re: How to implement custom llistfield having alternate color in row

Can you plz check the edited one and let me know.. Sorry. Wont do such mistakes again. Will try to Avoid. Thank you..
Please use plain text.
Developer
ravi1989
Posts: 773
Registered: ‎07-21-2012
My Carrier: 0

Re: How to implement custom llistfield having alternate color in row

Hello sir ..focus is not display on row  i change my code..

package com.black.applicationloader;

import java.util.Vector;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

import com.black.blackinterface.BlackInterface;
import com.black.common.BaseScreen;
import com.black.components.CustomListField;

public class BlackSecondScreen extends BaseScreen implements BlackInterface,ListFieldCallback {
	
	 private static final String[] _elements = {"First element", "Second element", "Third element", "Fourth element", "Fifth element"};
	 private Vector _listElements = new Vector(_elements.length, 1);
	 int columnWidth  = Display.getWidth()/4;
	
	BlackSecondScreen(){
		
		ListField colourList = new ListField();
		
		 colourList.setCallback(this);
		  int elementLength = _elements.length;
		 for(int count = 0; count < elementLength; ++count)
	        {
	            colourList.insert(count);
	            this.insert(_elements[count], count);
	        }
		 
		 add(colourList);

		
	}

	public void createComponents() {
		// TODO Auto-generated method stub
		
	}

	public void layoutComponents() {
		// TODO Auto-generated method stub
		
	}

	public void initializeListeners() {
		// TODO Auto-generated method stub
		
	}

	public void setComponentsXYMargins() {
		// TODO Auto-generated method stub
		
	}

	public void drawListRow(ListField listField, Graphics graphics, int index,
			int y, int width) {
		if(index%2  == 0  ){
			graphics.setColor(Color.PERU); 
			graphics.fillRect(0, y, width, listField.getRowHeight());
			}else{
			graphics.setColor(Color.WHEAT); 
			graphics.fillRect(0, y, width, listField.getRowHeight());
			}
			//graphics.fillRect(0,0,width,y);
			 
			int xpos = 0;int ypos = 0;
			graphics.setFont(Font.getDefault());// please set a font value
			// this is first column text
			graphics.setColor(Color.BLACK); 
			graphics.drawText("column1",xpos,y);
			xpos +=  columnWidth;
			 
			graphics.setColor(Color.RED); 
			graphics.drawText("column2",xpos,y);
			 
			xpos +=  columnWidth;
			graphics.drawText("column3",xpos,y);
			 
			xpos +=  columnWidth;
			 
			graphics.drawText("column4",xpos,y);
		
	}

	public Object get(ListField listField, int index) {
		// TODO Auto-generated method stub
		 return _listElements.elementAt(index);
	}

	public int getPreferredWidth(ListField listField) {
		// TODO Auto-generated method stub
		return Graphics.getScreenWidth();
	}

	public int indexOfList(ListField listField, String prefix, int start) {
		// TODO Auto-generated method stub
		return _listElements.indexOf(prefix, start);
	}
	
	public void insert(String toInsert, int index) 
    {
        _listElements.insertElementAt(toInsert, index);
    }
	
	 public void erase() 
	    {
	        _listElements.removeAllElements();
	    }
	

}

 

Please use plain text.