Re: J2ME - List and Font
On Tue, 20 Sep 2011 15:18:58 -0700, Roedy Green wrote:
msglist.setFont(0, f);
see http://mindprod.com/jgloss/sscce.html
Good point.
I don't see how this could compile. Your MyMsgList class has no setFont
method.
I think I should have been more clear about the fact, that this is j2me
code. The myListClass is extending the List class from
javax.microedition.lcdui.* and setFont() is a method to List.
http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/
List.html
I have uploaded the code to www.tj-software.dk/j2me/sms.zip.
And, I have found a solution to my problem, but I still don't understand
my original problem, so if someone can enlighten me, I would much
appreaciate.
So, if I do this:
Font f = Font.getFont(0, 0, Font.SIZE_LARGE);
msglist = new myListClass("messages");
/* appends elements to list here */
for (int i=0;i<msglist.size();i++) {
msglist.setFont(i, f);
}
msglist.setCommandListener(this);
display.setCurrent(msglist);
then will all element in the list be with large font. That's good and
proves it's working.
But if I want to make my list class do the job of making the font large,
I (did) think I have to do something like this.
public final class myListClass extends List {
rec_class recs;
Vector data;
Font f = Font.getFont(0, 0, Font.SIZE_LARGE);
public myListClass (String lname)
{
super (lname, Choice.IMPLICIT);
recs = new rec_class(lname);
myRebuild();
}
public void myRebuild () {
deleteAll();
data = recs.getrecords();
int i = 0;
for (Enumeration e = data.elements(); e.hasMoreElements(); ) {
append((String)e.nextElement(), null);
setFont(i++, f);
}
}
}
It compile, but when running, it throws and exception -
"java.lang.ArrayIndexOutOfBoundsException". I expect the array is the
lists array of elements. I don't understand why there is a problem,
because after appending an element, I think it should be possible to use
to it.
But I found a solution -
for (Enumeration e = data.elements(); e.hasMoreElements(); ) {
append((String)e.nextElement(), null);
//setFont(i++, f);
}
for (int i=0;i<size();i++) {
setFont(i, f);
}
ehhh, what? I have no idea why, but it works.
--
Thomas Jensen, Denmark