cp5 - checkbox.removeItem How to dynamically generate/delete a checkbox and items?

Hi, I'm trying to generate a checkbox when a search function is called which adds a new checkbox item for each found search item to be able to select one or more items from that search which I've got working. but now I want to be able to reset the search and also delete the whole checkbox and add again with new search items. so I've tried using checkbox.removeItem() or checkbox.deactivateAll() but that doesnt seem to actually delete the checkbox. see parts of code below, any thoughts? would really appreciate some advice, have tried for hours :) thanks.

//// CODE EXTRACT: ////

String[] foundUsers; ControlP5 cp5; CheckBox checkbox;

//// SEARCH FUNCTION ADDING CHECKBOX ITEMS PER FOUND USER //// void search() { searchQuery = cp5.get(Textfield.class, "firstName").getText()+" "+cp5.get(Textfield.class, "lastName").getText(); println(searchQuery);

if (searchQuery.equals(" ")) { textFont(font, 10); text("No runner found.", 50, 100); } else { search_results = searchPersonList(searchQuery); textFont(font, 12); int textSpace = 0;

checkbox = cp5.addCheckBox("checkbox").setPosition(100, 100).setSize(30, 30).setItemsPerRow(1);

foundUsers = new String [search_results.size()];

for (int i = 0; i < search_results.size(); i++) 
{ 
  textSpace = (height-700)/search_results.size(); 

  foundUsers[i] = search_results.get(i).getFullName()+"'s Great North Runs: ";

  //      println(foundUsers[i]+" - runs"+search_results.get(i).racelist);

  ArrayList<Race> races = search_results.get(i).getRaceListArrayList();

  String raceYears = "";
  for (int j = 0; j < races.size (); j++)
  {
    raceYears += races.get(j).getYear() + " ";
  }

  foundUsers[i] += raceYears;

 // println("in for loop: "+foundUsers[i]+" - "+i);
  checkbox.addItem(foundUsers[i], i);
}

//    println("outside loop: "+foundUsers.length);
//    println("outside loop: "+foundUsers[i-1]+" - "+(i-1));

} }

//// CLEAR FUNCTION TO RESET GUI ////

void clear() { cp5.get(Textfield.class, "firstName").clear(); cp5.get(Textfield.class, "lastName").clear();

for (int i =foundUsers.length-1; i>=0; i--) { println("delete "+foundUsers[i]); println(checkbox.getItems());

checkbox.removeItem(foundUsers[i]);

} }

Tagged:

Answers

  • sorry, I'm obviously new here and don't know how to post code properly... oops

  • Thanks GoToLoop. Proper code below. Does this work better? :)

      
        ControlP5 cp5;
        String[] foundUsers;
        CheckBox checkbox;
        
        
        // SEARCH FUNCTION ADDING CHECKBOX ITEMS PER FOUND USER
        void search()
        {
          searchQuery = cp5.get(Textfield.class, "firstName").getText()+" "+cp5.get(Textfield.class, "lastName").getText();
          println(searchQuery);
        
          if (searchQuery.equals(" "))
          {
            textFont(font, 10);
            text("No runner found.", 50, 100);
          } 
          else
          {
            search_results = searchPersonList(searchQuery);
            textFont(font, 12);
            int textSpace = 0;
        
            checkbox = cp5.addCheckBox("checkbox").setPosition(100, 100).setSize(30, 30).setItemsPerRow(1);
        
            foundUsers = new String [search_results.size()];
        
            for (int i = 0; i < search_results.size(); i++) 
            { 
              textSpace = (height-700)/search_results.size(); 
        
              foundUsers[i] = search_results.get(i).getFullName()+"'s Great North Runs: ";
        
              ArrayList races = search_results.get(i).getRaceListArrayList();
        
              String raceYears = "";
              for (int j = 0; j < races.size (); j++)
              {
                raceYears += races.get(j).getYear() + " ";
              }
        
              foundUsers[i] += raceYears;
        
              println("in for loop: "+foundUsers[i]+" - "+i);
              checkbox.addItem(foundUsers[i], i);
            }
          }
        }
        
        
        // CLEAR FUNCTION TO RESET GUI
        
        void clear() {
          cp5.get(Textfield.class, "firstName").clear();
          cp5.get(Textfield.class, "lastName").clear();
        
          for (int i =foundUsers.length-1; i>=0; i--)
          {
            println("delete "+foundUsers[i]);
            println(checkbox.getItems());
        
            checkbox.removeItem(foundUsers[i]);
          }
        }
    
    
Sign In or Register to comment.