Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Add / remove button

$
0
0

Ive created the following example for adding buttons with "+" and removing buttons with "-". But I cant find out how to remove a button. Only adding. It would be perfect if something like removeButton() existed but it doesnt so need another solution. Any suggestion is very much apreciated!

import controlP5.*;
ControlP5 cp5;

int listNr = 0;
StringList list;

void setup() {
  size(400, 600);
  cp5 = new ControlP5(this);

  list = new StringList();
  list.append(str(listNr));

  cp5.addButton("listNumber: " + listNr)
    .setValue(0) // detta värde skickas när den trycks ner
    .setPosition(100, 100+(listNr*30))
    .setSize(200, 19)
    ;
}

void draw() {
}

void keyReleased() {
  if (key == '+') {
    listNr++;
  } else if (key == '-') {
    listNr--;
  }

  // if the button doesnt exist - add a new one
  for (int i = 0; i < list.size(); i++) {
    if (listNr == list.size()-1) {
      println("DO NOTHING");
      break; // leave loop
    } else if (listNr > list.size()-1) {
      println("ADD BUTTON");
      cp5.addButton("listNumber: " + listNr)
        .setValue(0)
        .setPosition(100, 100+(listNr*30))
        .setSize(200, 19)
        ;
      list.append(str(listNr));
      break; // leave loop
    } else if (listNr < list.size()-1) {
      println("REMOVE BUTTON");

      /* HERE I WANT TO DO SIMILIAR THING AS ABOVE BUT REMOVE A BUTTON
       cp5.removeButton("listNumber: " + listNr); // BUT THIS DOESNT WORK
       */

      list.remove(listNr);
      break; // leave loop
    }
  }

  println("listNr: " + listNr + " LISTSIZE: " + list.size());
  println("LIST: " + list);
  println("====================");
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles