# I want the listed buttons to work only one at a time

**URL:** <https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249>\
**Category:** Libraries\
**Created:** [June 19, 2023, 12:28pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249 "2023-06-19T12:28:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 19, 2023, 12:28pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/1 "2023-06-19T12:28:54Z")

</div>

Hello, I am a student who is making a control program using Processing 4.  
The control program has 128 listed buttons, and when a button is pressed, the pin connected to the corresponding button in the driving circuit turns on. Currently, there is a program, but I am trying to modify it so that only one button out of 128 is On. (Currently, all buttons can be turned on, if another button is pressed while one is turned on, only the newly pressed button needs to be turned on)  
I’m not sure how to fix it, so I’m asking. Thank you for your reply.  
here is a code under the line

```auto
void gui_setup(){
cp5 = new ControlP5(this);

  electrodeBox = cp5.addCheckBox("electrodeBox")
    .setPosition(electrode_checkbox_x, electrode_checkbox_y)
    .setSize(electrode_checkbox_width, electrode_checkbox_height)
    .setItemsPerRow(16)
    .setSpacingColumn(0)
    .setSpacingRow(0)
    .setImages(electrode_unclick, electrode_unclick, electrode_click);

  for (int j=0; j<16; j++)
  {
    for (int i=0; i<8; i++)
    {
      electrodeBox.addItem(char(j+'A')+str(i+1), i+j*8);
    }
  }
}
void electrodeBox(float[] float_electrode)
{
  boolean[x] electrode=new boolean[128];
  for (int i=0; i<128; i++)
  {
    if (float_electrode[i]==0.0)electrode[i]=false;
    else electrode[i]=true;
  }
  hvout_setting(electrode_to_hvout(electrode));
}

```

---

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 19, 2023, 12:30pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/2 "2023-06-19T12:30:40Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/f/6f3c92e4263958cb650b673940718212a7257bf7.png)

---

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 19, 2023, 12:31pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/3 "2023-06-19T12:31:12Z")

</div>

this is my program image

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 19, 2023, 2:40pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/4 "2023-06-19T14:40:56Z")

</div>

please post a runable version of your code

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 19, 2023, 2:58pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/5 "2023-06-19T14:58:56Z")

</div>

I think what you are looking for is called a ‘radio group’. The following code was taken from the ControlP5 Examples (ControlP5group):

```auto
import controlP5.*;

ControlP5 cp5;

void setup() {
  size(800, 400);
  cp5 = new ControlP5(this);
  cp5.addRadioButton("radio")
    .setPosition(100, 30)
    .setSize(40, 30)
    .addItem("black", 0)
    .addItem("red", 1)
    .addItem("green", 2)
    .addItem("blue", 3)
    .addItem("grey", 4)
    ;
}

void draw() {
  background(0);
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    println("got an event from group "
      +theEvent.getGroup().getName()
      +", isOpen? "+theEvent.getGroup().isOpen()
      );
  }
}

```

**Modified to add a grid of buttons:**

```auto
import controlP5.*;

ControlP5 cp5;
RadioButton radio;

final int _numCols = 16;
final int _numRows = 8;

void setup() {
  size(750, 350);
  background(209);
  cp5 = new ControlP5(this);
  radio = cp5.addRadioButton("radio");
  for (int x = 0; x < _numCols*_numRows; x++) {
    radio.setItemsPerRow(_numCols);
    radio.setPosition(30, 30);
    radio.setSize(40, 30);
    radio.addItem(str(x), x);
  }
}

void draw() {
}

void radio(int a) {
  println("radio id: ", a);
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    println("event from group "
      +theEvent.getGroup().getName()
      +", isOpen? = "+theEvent.getGroup().isOpen()
      );
  }
}

```

---

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 20, 2023, 5:49am UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/8 "2023-06-20T05:49:51Z")

</div>

The original code is too long, so I used only the necessary parts, so it doesn’t seem to work properly. Sorry.

---

<div class="post-metadata">

**Author:** ![Hyunwoo](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hyunwoo](https://discourse.processing.org/u/Hyunwoo)\
**Post date:** [June 20, 2023, 5:50am UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/9 "2023-06-20T05:50:32Z")

</div>

thank you for the reply  
I was able to edit it using Radiobutton.  
Additionally, I am trying to modify it so that multiple selections can be made when pressing the SHIFT key, void keyPressed(){  
} How should I define it inside the function??

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 22, 2023, 7:14pm UTC](https://discourse.processing.org/t/i-want-the-listed-buttons-to-work-only-one-at-a-time/42249/10 "2023-06-22T19:14:29Z")

</div>

continued in new thread
