×

Discussion Board

Results 1 to 9 of 9

Thread: List and Font

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    List and Font

    I have a problem with Font in a list. To me it looks like a bug in the java libraries, but I'm a newbee, so maybe it's just me not understanding it.

    I can change the font size in my list. But if I add elements to the list, it throws an ArrayIndexOutOfBoundsException. Take a look at the rebuild command in the code. I have tried a lot of things to get around it, including deleting all the elements and loading them again, but with no luck.

    The error says:

    java.lang.ArrayIndexOutOfBoundsException
    at javax.microedition.lcdui.ChoiceGroup.insertImpl(ChoiceGroup.java:1420)
    at javax.microedition.lcdui.ChoiceGroup.append(ChoiceGroup.java:397)
    at javax.microedition.lcdui.List.append(List.java:429)
    at listtest.commandAction(listtest.java:70)
    at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(Display.java:2093)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(Display.java:2929)
    at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(DefaultEventHandler.java:297)
    at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(AutomatedEventHandler.java:667)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(DefaultEventHandler.java:711)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:608)


    Any ideas what is wrong and how to get around it? Do I really have to delete the list and create a new one?

    Code:
    import java.util.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    
    public class listtest extends MIDlet implements CommandListener {
        private Display display;
        private List list;
        private Command exitcmd, reloadcmd;
        private Vector v;
        Font f;
    
        public void startApp() {
    
            reloadcmd = new Command("Reload", Command.SCREEN, 1);
            exitcmd = new Command("Exit", Command.EXIT, 2);
    
            f = Font.getFont(0, 0, Font.SIZE_LARGE);
    
            v = new Vector(1,1);
            v.addElement("string1");
            v.addElement("string2");
            v.addElement("string3");
            v.addElement("string4");
    
            list = new List("", Choice.IMPLICIT);
            list.addCommand(reloadcmd);
            list.addCommand(exitcmd);
            list.setCommandListener(this);
    
            int i = 0;
            for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
                list.append((String)e.nextElement(), null);
                // ArrayIndexOutOfBoundsException, if setting font here...
                // list.setFont(i, f);
            }
    
            for (i=1 ; i<list.size() ; i++ ) {
                // It's okay here...
                list.setFont(i, f);
            }
    
            display = Display.getDisplay(this);
            display.setCurrent(list);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    
        public void commandAction (Command c, Displayable d) {
            if (c == reloadcmd) {
    
    
    //            list.deleteAll();
    //            int i = 0;
    //            for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
    //                list.append((String)e.nextElement(), null);
    //            }
    
                list.append("test", null);
            
    //            for (i=0 ; i<list.size() ; i++ ) {
    //                if (!list.getFont(i).equals(f)){
    //                    list.setFont(i, f);
    //                }
    //            }
    
                display.setCurrent(list);
            
            }
            
            if (c == exitcmd) {
                destroyApp(true);
                notifyDestroyed();
            }
        }
    }

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: List and Font

    Hello lianergoist,

    could you specify please which Nokia device or SDK emulator results in the Exception?

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

  3. #3
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: List and Font

    I suspect about your font, try to give some face and style too while you are creating the same. Then you go for testing it.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Re: List and Font

    Quote Originally Posted by raj_J2ME View Post
    I suspect about your font, try to give some face and style too while you are creating the same. Then you go for testing it.
    Like this?

    f = Font.getFont(Font.FACE_SYSTEM , Font.STYLE_PLAIN, Font.SIZE_LARGE);

    Well, it didn't change anything - and it shouldn't. The API reference says:

    The values of these constants are arranged so that zero is valid for each attribute and can be used to specify a reasonable default font for the system. For clarity of programming, the following symbolic constants are provided and are defined to have values of zero:

    * STYLE_PLAIN
    * SIZE_MEDIUM
    * FACE_SYSTEM
    But thanks for the suggestion...

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Re: List and Font

    Quote Originally Posted by r2j7 View Post
    Hello lianergoist,

    could you specify please which Nokia device or SDK emulator results in the Exception?

    Regards,
    r2j7
    Well, the error output posted earlier was from Netbeans6.9 using WTK2.5.2 and DefaultColorPhone profile. Building and running the program from ktoolbar give this error:

    java.lang.ArrayIndexOutOfBoundsException
    at javax.microedition.lcdui.ChoiceGroup.insertImpl(+456)
    at javax.microedition.lcdui.ChoiceGroup.append(+25)
    at javax.microedition.lcdui.List.append(+9)
    at listtest.commandAction(+71)
    at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)

    When running the program on my Samsung B2100 phone, I get no errors, but the new line is not appended (list.append("test", null) and the 4 elements are all with standard size (not large, as 3 of them were before running the rebuild command).

  6. #6
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: List and Font

    Maybe you might want to do some System.out.println within your :

    for (i=1 ; i<list.size() ; i++ ) {
    // It's okay here...
    list.setFont(i, f);
    }

    just to check where it al goes wrong ?
    Also shouldn't the variable i be initialised with 0 ?

    Personally looking at your error I think something is going wrong here :

    for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
    list.append((String)e.nextElement(), null);
    // ArrayIndexOutOfBoundsException, if setting font here...
    // list.setFont(i, f);
    }

    you might want to do something like this :

    Enumeration e = v.elements();
    while( e.hasNextElement()
    {
    list.append((String)e.nextElement(), null);
    // ArrayIndexOutOfBoundsException, if setting font here...
    // list.setFont(i, f);
    }
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Re: List and Font

    Quote Originally Posted by Tiger79 View Post
    Maybe you might want to do some System.out.println within your :

    for (i=1 ; i<list.size() ; i++ ) {
    // It's okay here...
    list.setFont(i, f);
    }

    just to check where it al goes wrong ?
    Well, it makes sense why I can't apply font size here:

    Code:
     
            int i = 0;
            for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
                list.append((String)e.nextElement(), null);
                // ArrayIndexOutOfBoundsException, if setting font here...
                // list.setFont(i, f);
            }
    but I can here:

    Code:
     
            for (i=1 ; i<list.size() ; i++ ) {
                // It's okay here...
                list.setFont(i, f);
            }
    when we look at what makes the exception, when running the reload command. The only thing that happens in the reload scope, is we append an element to the list. That's not normally a problem, and if we comment out the list.setFont code, there is no problem appending another element. But if we use setFont first, and then append an element, we get the exception. So, in this code:

    Code:
     
            int i = 0;
            for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
                list.append((String)e.nextElement(), null);
                // ArrayIndexOutOfBoundsException, if setting font here...
                list.setFont(i, f);
            }
    the first element is appended successfully, and setFont will also apply font successfully, but next element will create an exception. I don't think it should, and I don't understand why, but it does. It is exactly what happens in the reload command scope:

    Code:
     public void commandAction (Command c, Displayable d) {
            if (c == reloadcmd) {
    
    
    //            list.deleteAll();
    //            int i = 0;
    //            for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
    //                list.append((String)e.nextElement(), null);
    //            }
    
                list.append("test", null);
            
    //            for (i=0 ; i<list.size() ; i++ ) {
    //                if (!list.getFont(i).equals(f)){
    //                    list.setFont(i, f);
    //                }
    //            }
    
                display.setCurrent(list);
            
            }

    Also shouldn't the variable i be initialised with 0 ?
    Well, yes. It was only to show the diffedrence in font size.

    Personally looking at your error I think something is going wrong here :

    for (Enumeration e = v.elements(); e.hasMoreElements(); i++ ) {
    list.append((String)e.nextElement(), null);
    // ArrayIndexOutOfBoundsException, if setting font here...
    // list.setFont(i, f);
    }

    you might want to do something like this :

    Enumeration e = v.elements();
    while( e.hasNextElement()
    {
    list.append((String)e.nextElement(), null);
    // ArrayIndexOutOfBoundsException, if setting font here...
    // list.setFont(i, f);
    }
    No, it's actually working well. It may be a little harder to read, but it does the same.
    Last edited by lianergoist; 2011-09-30 at 12:45. Reason: typo again

  8. #8
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: List and Font

    hhmm, sorry then I don't have a clue :P
    It does sound stupid though that you have to set the Font for every specific item within a list (or choicegroup). It would sound more logical to me (especially to keep a list looks uniform) to do that once for the whole list, as it being a setting/parameter of the whole list which will be passed down to its children (thus the entries within the list)... Anyhow thats some personal mumbling
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Re: List and Font

    Quote Originally Posted by Tiger79 View Post
    It does sound stupid though that you have to set the Font for every specific item within a list (or choicegroup).
    Yes, but doing it on individual elements let you 'highligt' the selected element. Well, that's maybe not a good reason, but... ;-)

    BTW - I have been looking a bit deeper into it. I'm not familiar with java (OO) design and terminology, so maybe I use some wrong terms here, but... the choicegroup documentation say:

    "Each element of a Choice is composed of a text string part, an Image part, and a font attribute that are all treated as a unit. "

    ... "as a unit". But when I ran some some test code in the debugger, font was not part of each element, but a part of the choicegroup. 'fontEls' is an unallocated array of Font objects in choicegroup until you use setFont() first time. Then it is created with a size equal the current number of elements in the list, and a font object is added to one of the array elements. So far, so good. What I would expect now was, when I used append(), the fontEls array were reallocated to hold the new number of elements. But we newer get so far, because using append() after fontEls is allocated, throws an ArrayIndexOutOfBoundsException.

    I don't see other solutions than deleting the list/choicegroup and create it again with the new size. And it's not a big problem to do that, because it it all happens so extremely fast, so no one will ever notice this ugly hack... ;-)

Similar Threads

  1. List not changing Font Size nor Font Types
    By smabres in forum Mobile Java General
    Replies: 2
    Last Post: 2008-09-17, 16:22
  2. How to change the font in the list box?
    By gigglie in forum Symbian
    Replies: 2
    Last Post: 2008-07-25, 12:51
  3. set font in list box
    By kingkiko in forum Symbian
    Replies: 0
    Last Post: 2006-04-16, 18:26
  4. Change font in list.
    By inforn in forum Mobile Java General
    Replies: 1
    Last Post: 2004-05-28, 07:32
  5. changing List Box Font
    By Nilesh_Kul in forum Symbian User Interface
    Replies: 0
    Last Post: 2003-04-20, 11:19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×