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: http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5listBox/ControlP5listBox.pde 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);
        add(s);
        s.init();
        show();
      }
    }
    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);
        ControlP5.printPublicMethodsFor(ListBox.class);
        cp5 = new ControlP5(this);
        l = cp5.addListBox("myList")
             .setPosition(0, 0)
             .setSize(382, 720)
             .setItemHeight(18)
             .setBarHeight(18)
             .setColorBackground(color(243, 45,98))
             .setColorActive(color(0))
             .setColorForeground(color(255, 100,0))
             ;

      l.captionLabel().toUpperCase(true);
      l.captionLabel().set("Companies");
      l.captionLabel().setColor(0xffff0000);
      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(PGraphics.java:4681)
at processing.core.PGraphics.textLineImpl(PGraphics.java:4669)
at processing.core.PGraphicsJava2D.textLineImpl(PGraphicsJava2D.java:1787)
at processing.core.PGraphics.textLineAlignImpl(PGraphics.java:4659)
at processing.core.PGraphics.text(PGraphics.java:4356)
at processing.core.PGraphics.text(PGraphics.java:4307)
at processing.core.PApplet.text(PApplet.java:13183)
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(PApplet.java:1236)
at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1229)
at processing.core.PApplet.handleMethods(PApplet.java:1423)
at processing.core.PApplet.handleDraw(PApplet.java:2401)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Unknown Source)

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

Answers

  • edited April 2015

    Try

    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++) {
                 try{
                  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.