ControlP5 Listbox ArrayIndexOutOfBoundsException

edited April 2015 in Library Questions

Hi. I encountered really a strange problem with using a ControlP5 ListBox in my program. I found an example of ListBox use from the official page of ControlP5 library: I did some simple changes like changing listbox width and height, setting different colors. The main difference consist in the fact I'm using an arrayList of Strings to populate listbox. Here is the complete code for this scetch:

           import javax.swing.*;
    PFrame f;
    public class PFrame extends JFrame {
      public SecondApplet s;
      public PFrame(int width, int height,ArrayList<String> companiesTxt) {
        setBounds(100, 100, width, height);
        s = new SecondApplet(companiesTxt);
    public class SecondApplet extends PApplet {
      ArrayList<String> companiesText;

    ControlP5 cp5;

    ListBox l;

    ControlWindow controlWindow;
      SecondApplet(ArrayList<String> cmpTxt){
        companiesText = cmpTxt;
      void setup(){
        size(400, 720);
        cp5 = new ControlP5(this);
        l = cp5.addListBox("myList")
             .setPosition(0, 0)
             .setSize(382, 720)
             .setColorBackground(color(243, 45,98))
             .setColorForeground(color(255, 100,0))

      l.captionLabel().style().marginTop = 3;
      l.valueLabel().style().marginTop = 3;

      for (int i=0;i != companiesText.size();i++) {

        ListBoxItem lbi = l.addItem(companiesText.get(i), i);
        lbi.setColorBackground(color(243, 45,98));


      void draw(){


When I try to scroll this listbox, I get an exception:

     java.lang.ArrayIndexOutOfBoundsException: 8217
at controlP5.BitFont.getGlyph(Unknown Source)
at processing.core.PGraphics.textCharImpl(
at processing.core.PGraphics.textLineImpl(
at processing.core.PGraphicsJava2D.textLineImpl(
at processing.core.PGraphics.textLineAlignImpl(
at processing.core.PGraphics.text(
at processing.core.PGraphics.text(
at processing.core.PApplet.text(
at controlP5.ControlFont.draw(Unknown Source)
at controlP5.Label.draw(Unknown Source)
at controlP5.Label$SinglelineLabel.draw(Unknown Source)
at controlP5.Label.draw(Unknown Source)
at controlP5.Button$ButtonView.display(Unknown Source)
at controlP5.Button$ButtonView.display(Unknown Source)
at controlP5.Controller.draw(Unknown Source)
at controlP5.ControllerGroup.drawControllers(Unknown Source)
at controlP5.ControllerGroup.draw(Unknown Source)
at controlP5.ControllerGroup.drawControllers(Unknown Source)
at controlP5.ControllerGroup.draw(Unknown Source)
at controlP5.ControlWindow.draw(Unknown Source)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at processing.core.PApplet$RegisteredMethods.handle(
at processing.core.PApplet$RegisteredMethods.handle(
at processing.core.PApplet.handleMethods(
at processing.core.PApplet.handleDraw(
at processing.core.PGraphicsJava2D.requestDraw(
at Source)

I'm almost desperate because I do not understand what can be wrong in this case? Please, can you give me any suggestions?


  • edited April 2015


    for (int i=0; i < companiesText.size(); i++) {

  • quark, thank for your answer, but there is not difference between != and < in this loop.

  • The problem was resolved. I was using some strings that are not aschii characters. GetGlyph function looks like this:

    public Glyph getGlyph(char c) { return glyphs[(int) (c)]; }

    So, when you want to use a listbox convert characters to aschii:

                      ` for (int i=0;i < companiesText.size();i++) {
                  ListBoxItem lbi = l.addItem(new String(companiesText.get(i).getBytes("US-ASCII"),"US-ASCII"), i);
                  lbi.setColorBackground(color(243, 45,98));
                 }catch(Exception e){
  • GoToLoop, I really used search and found no result :(((

Sign In or Register to comment.