Scrollable list to Bang

Please make sure you format your code. You can edit your post, select your code and click on the </> button aka. the button to format your code.

I changed the name of your variable for a list as a single letter name “l” is not very descriptive. You should called it something meaningful, based on the functionality of this object in your program.

Have a look at this example. It is one way to do things and hopefully it will provide you some ideas that you can exploit yourself. The trick is to detect when the list was clicked. You will have a variable that stores the selection. Notice I init this variable to NO_SELECTED in case the user runs the program and hits the bang button right way. Any other question or clarification, feel free to ask.

Kf

import controlP5.*;
import java.util.*;
import processing.sound.*;


final int NO_SELECTED = -1;
SoundFile file1;
SoundFile file2;

ControlP5 cp5;
int listOneSelection;
List list;

void setup() {
  file1 = new SoundFile(this, "sound1.wav");
  file2 = new SoundFile(this, "sound2.wav");
  cp5 = new ControlP5(this);

  background(0);
  size(1450, 900);

  list = Arrays.asList("Option1", "Option2");
  listOneSelection=NO_SELECTED;

  cp5.addScrollableList("list1")
    .setPosition(250, 500)
    .setSize(100, 40)
    .setBarHeight(20)
    .setItemHeight(20)
    .addItems(list)
    //.setType(ScrollableList.DROPDOWN)
    .setLabel("List 1 ");

  cp5.addBang("a_bang1")
    .setPosition(1100, 500)
    .setSize(40, 40)
    .setLabel("1")
    ;
  cp5.addBang("a_bang2")
    .setPosition(1150, 500)
    .setSize(40, 40)
    .setLabel("2")
    ;
}

void draw() {
}

void list1(int n) {
  listOneSelection=n;
}

void a_bang1() {

  manageSoundSelection();

  //if (1==1) // this is where I want to ask if the list1 has picked option 1){
  //  file1.play();
  //else if (1==1) // has option 2 been selected){
  //  file2.play();
}


void a_bang2() {

  manageSoundSelection();

  //if (1==1) // this is where I want to ask if the list1has picked option 1){
  //  file2.play();
  //else if (1==1) // has option 2 been selected){
  //  file1.play();
}


void manageSoundSelection() {
  switch(listOneSelection) {
  case NO_SELECTED:
    println("Nothing to do. Please make sure you choose an item in list 1");
    break;
  case 0:
  case 1:
    println("You have selected item " + listOneSelection + " fro list1");
    break;
  default:
    break;
  }
}

2 Likes