How to write code to press multiple Radiobuttons at once (button grid)

Hello everyone

I have created 128 switches as radiobuttons and I want them to work only one at a time, and I want to be able to press SHIFT and select a switch and have multiple alternatives, how should I code it?

Below is the code created with the Radiobutton.

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()
      );
  }
}
//Below are the functions I need to add
//void keyPressed (){
 // if (keyCode==SHIFT) 

//}

//void keyReleased()
  {
  //  if (keyCode==SHIFT) 
   
//  } 

The function of a radiobutton is to allow only one selection, which was your initial question. Think of selecting a radio station on your car radio; each button goes to a single station. Now you want to use radiobuttons for multiple selections. In order to have that behavior it would likely require a custom control. If you want multiple selections then checkboxes would probably be a better choice. In summary, radiobuttons for a single choice and checkboxes for multiple selections for normal situations. It could be difficult to combine the two without using custom code. https://en.wikipedia.org/wiki/Radio_button

5 Likes

Thanks for the answer!
I’ll try to define a user function to combine checkboxes and radio buttons!

you can also write your own

// https://discourse.processing.org/t/how-to-write-code-to-press-multiple-radiobuttons-at-once/42263/3

/*
 Select one button
 Hold shift key to select multiple keys
 Esc key to reset
 i to inverse grid
 To Do: store the matrix and make a replay
 */

ArrayList<Button> buttons = new ArrayList ();

boolean shiftIsDown = false;

// ---------------------------------------------------------------------------------------

void setup () {
  size (1000, 600);

  // init grid
  for (int x = 0; x<8; x++) {
    for (int y = 0; y<8; y++) {
      Button btn = new Button (x*40+30,
        y*40+30);
      buttons.add(btn);
    }//for
  }//for

  background(111);
}//func

void draw () {
  background(255);

  // show yellow box
  showYellowBox("Demo for Button Grid \n\nMouse select\nshift with mouse - multi select\ni - inverse\nEsc - reset\nX - quit  ");

  // show button grid
  for (Button btn : buttons) {
    btn.display();
  }//for

  // show as text
  fill(0);
  int i=0;
  for (int x = 0; x<8; x++) {
    for (int y = 0; y<8; y++) {
      Button btn=buttons.get(i);
      text(btn.selectedToString(),
        x*7+width/2-110, y*7+height/2-110);
      i++;
    }//for
  }//for
}//func

// ---------------------------------------------------------------------------------------
// Inputs

void mousePressed() {
  // when we don't have shift
  if (!shiftIsDown) {
    // all off
    for (Button btn : buttons) {
      btn.selected=false;
    }
  }//if

  //search
  for (Button btn : buttons) {
    if (btn.inside()) {
      btn.selected=true; // or toggle
    }//if
  }//for
}//func

void keyPressed() {
  if (keyCode==SHIFT) {
    shiftIsDown=true;
  } else if (key==ESC) {
    // reset all
    for (Button btn : buttons) {
      btn.selected=false;
    }
  } else if (key=='i') {
    // inverse all
    for (Button btn : buttons) {
      btn.selected= ! btn.selected; // inverse
    }
  } else if (key=='X') {
    // Quit
    exit();
  }// else if

  key=0;//kill Esc
} // func

void keyReleased() {
  if (keyCode==SHIFT) {
    shiftIsDown=false;
  } //if
} //func

// ------------------------------------------------------------------
// Other Tools

void showYellowBox(String text_) {
  // yellow box
  fill( #F1F743 );//YELLOW
  rect ( width - 297, 19,
    262, 366);

  // black frame
  noFill();
  stroke(0);
  float a1 = 4;
  rect ( width - 297+a1, 19+a1,
    262-a1*2, 366-a1*2);
  noFill();

  //text
  fill(0);
  textSize(14);
  text (text_,
    width - 290+2, 19+5,
    270, 900);
  fill(255);
} //func

// ====================================================================

class Button {

  final float buttonSize = 35;

  float  x, y;

  boolean selected=false;

  // constr
  Button (float x_, float y_) {
    x=x_;
    y=y_;
  }// constr

  void display() {
    if (selected)
      fill(222);
    else
      fill(111);

    stroke(0);
    rect(x, y,
      buttonSize, buttonSize);
  }

  boolean inside() {
    return
      mouseX>x &&
      mouseX<x+buttonSize &&
      mouseY>y &&
      mouseY<y+buttonSize;
  }//method

  String selectedToString() {
    if (selected)
      return "x";
    else
      return ".";
  }
  //
}//class

// =================================================================

keywords: select buttons button grid multi select on off buttons multiple radiobuttons

2 Likes

thank you! i will try to fix my code using your code :grinning:

1 Like