controlP5: controller already exists with RadioButton with same options

Hi! I think I solved this in the past but can’t remember how atm. The code:

import controlP5.*;

ControlP5 cp5;

void setup() {
  cp5 = new ControlP5(this);
  RadioButton r1 = cp5.addRadioButton("foo")
    .setPosition(0, 0)
    .addItem("x", 1)
    .addItem("y", 2); 
  RadioButton r2 = cp5.addRadioButton("bar")
    .setPosition(50, 0)
    .addItem("x", 1)
    .addItem("y", 2); 
}
void draw() { }

The problem is that only one RadioButton is shown because it doesn’t like having more buttons with the same name. The error:

Jan 14, 2020 6:00:45 PM controlP5.ControlP5 checkName
WARNING: Controller with name "/x" already exists. overwriting reference of existing controller.
Jan 14, 2020 6:00:45 PM controlP5.ControlP5 checkName
WARNING: Controller with name "/y" already exists. overwriting reference of existing controller.

This issue is somewhat related.

One ugly fix is to rename x and y to something like x0, y0, x1, y1. Was there a different way to give buttons unique ids but repeating labels?

Thank you :slight_smile:

Mmm… not clean but the gui looks right:

  RadioButton r1 = cp5.addRadioButton("foo")
    .setPosition(0, 0)
    .addItem("x0", 1)
    .addItem("y0", 2); 
  r1.getItem(0).setLabel("x");
  r1.getItem(1).setLabel("y");
    
  RadioButton r2 = cp5.addRadioButton("bar")
    .setPosition(50, 0)
    .addItem("x1", 1)
    .addItem("y1", 2);
  r2.getItem(0).setLabel("x");
  r2.getItem(1).setLabel("y");
}

Or a tiny bit more generic:

  RadioButton r1 = cp5.addRadioButton("foo")
    .setPosition(0, 0)
    .addItem("x0", 1)
    .addItem("y0", 2); 
  for(Toggle t : r1.getItems()) {
    t.setLabel(t.getLabel().substring(0, 1));
  }
    
  RadioButton r2 = cp5.addRadioButton("bar")
    .setPosition(50, 0)
    .addItem("x1", 1)
    .addItem("y1", 2);
  for(Toggle t : r2.getItems()) {
    t.setLabel(t.getLabel().substring(0, 1));
  }