11-28-2009 05:42 AM - edited 11-28-2009 05:57 AM
Hi all,
I m creating a ListField with Checkboxex from How To - Create a ListField with check boxes
But I m not getting how to wrap Text in a row in that ListField.
I referred the code http://pastie.org/282759/wrap but the Checkbox is not coming in ListField.
I think we cant mess up the code in these 2 links.
I referred the thread
Text Wrapping for text in List Field items
Here its written as
If all you are displaying is text, then I would go with the approach suggested by rab.
I m displaying Text only in ListField which I want to wrap if it doesnt fit within device screen.
But i m not getting "approach suggested by rab" in which How to calculate?
String [] linesToDraw = calculate the number of lines depending on the row width
I m stucked at this so any details on this issue ?
11-28-2009 10:16 AM
You calculate the width of the text using Font.getAdvance(String text)
See the API docs.
12-01-2009 02:53 AM
I got the no. of lines properly according to row width & stored it in a Vector & i m doing
int fontHeight = this.getFont().getHeight();
for (int i = 0; i < linesToDraw .size(); i++)
{
textToDraw = (String)linesToDraw.elementAt(i);
graphics.drawText(textToDraw, xpos, ypos, 0 , width);
ypos += fontHeight;
}now its drawing the lines but over the previous one as by default listField.setRowHeight() is taking only one row. If there r 2 rows then I write setRowHeight(getFont().getHeight()*2) in drawListRow() but then it goes in loop
If I write setRowHeight() while creating ListField then it comes properly but for all rows same row height is set.
I gets the String to draw at Runtime & the row height should be variable for different rows then how can I setRowHeight() with what parameter & in which method ?
12-01-2009 04:46 AM
There is no public API to have a variable height ListField.
There are a number of Threads on this forum that discuss this and the various options, search round for them.
12-02-2009 01:55 AM
Hey Shreyas can you guide me to calculate number of lines required? and also to create linesToDraw collection?
Thanks in Advance
12-02-2009 02:51 AM
The Logic is like..
* split text on words
* create string array and while words array not empty do
o create string lineStr
o while getFont().getAdvance(lineStr+" "+words[i]) < row width move words[i] to lineStr
in the end you will get:
* String[] lines with lines for row
* Row height = lines.lenght * (getFont().getHeight() + topMargin + bottomMargin)
& do g.drawText() like in my previous post.
But i m confused from where to call setRowHeight() as the Row Height is varible for different Rows.
12-02-2009 03:01 AM - edited 12-02-2009 03:03 AM
setRowHeight method apply on your ListField.
12-02-2009 04:33 AM
Thanks for thr reply but list.setRowHeight(45); is hardcoded there
& In my case the row height is variable for different rows & I gets the String to draw at Runtime.
So if I write setRowHeight(fontHeight*2) in drawListRow() like
public void drawListRow(ListField list, Graphics graphics, int index, int y, int w)
{
................................
...............................
for (int i = 0; i < linesToDraw.size(); i++) { String textToDraw = (String)linesToDraw.elementAt(i); list.setRowHeight(fontHeight); graphics.drawText(textToDraw, 0, y, 0, w); y += fontHeight; }
}
but then it goes in loop
If I write setRowHeight() while creating ListField then it comes properly but for all rows same row height is set & I have t o give a fixed value there.
So how to write setRowHeight() with variable value & where to write it?
12-02-2009 04:37 AM
The code which provided will be just for reference and you may need to change a bit which suites your requirement.
Following one thread is also about setting variable height in Listfield and it is discussed about the performace too.. you just give a look at it.
http://supportforums.blackberry.com/t5/Java-Develo