Is there a better way to write this piece of code?

Is there a better way to write this piece of code? These belong to my DropDownList code. These lines are needed to bring the clicked list to front.
Thank You.

 if (d1.isMousePressed()) {

    d1.bringToFront();
  }
  if (d2.isMousePressed()) {

    d2.bringToFront();
  }
  if (d3.isMousePressed()) {

    d3.bringToFront();
  }
  if (d4.isMousePressed()) {

    d4.bringToFront();
  }
  if (d5.isMousePressed()) {

    d5.bringToFront();
  }
  if (d6.isMousePressed()) {

    d6.bringToFront();
  }
  if (d7.isMousePressed()) {

    d7.bringToFront();
  }
  if (d8.isMousePressed()) {

    d8.bringToFront();
  }
   if (d9.isMousePressed()) {

    d9.bringToFront();
  }
  if (d10.isMousePressed()) {

    d10.bringToFront();
  }
  if (d11.isMousePressed()) {

    d11.bringToFront();
  }
  if (d12.isMousePressed()) {

    d12.bringToFront();
  }
  if (d13.isMousePressed()) {

    d13.bringToFront();
  }
  if (d14.isMousePressed()) {

    d14.bringToFront();
  }
  if (d15.isMousePressed()) {

    d15.bringToFront();
  }
  if (d16.isMousePressed()) {

    d16.bringToFront();
  }
  if (d17.isMousePressed()) {

    d17.bringToFront();
  }
  if (d18.isMousePressed()) {

    d18.bringToFront();
  }
   if (d19.isMousePressed()) {

    d19.bringToFront();
  }
  if (d20.isMousePressed()) {

    d20.bringToFront();
  }
  if (d21.isMousePressed()) {

    d21.bringToFront();
  }
  if (d22.isMousePressed()) {

    d22.bringToFront();
  }
  if (d23.isMousePressed()) {

    d23.bringToFront();
  }
  if (d24.isMousePressed()) {

    d24.bringToFront();
  }
  if (d25.isMousePressed()) {

    d25.bringToFront();
  }
  if (d26.isMousePressed()) {

    d26.bringToFront();
  }
  if (d27.isMousePressed()) {

    d27.bringToFront();
  }
  if (d28.isMousePressed()) {

    d28.bringToFront();
  }
   if (d29.isMousePressed()) {

    d29.bringToFront();
  }
  if (d30.isMousePressed()) {

    d30.bringToFront();
  }
  if (d31.isMousePressed()) {

    d31.bringToFront();
  }
  if (d32.isMousePressed()) {

    d32.bringToFront();
  }
  if (d33.isMousePressed()) {

    d33.bringToFront();
  }
  if (d34.isMousePressed()) {

    d34.bringToFront();
  }
  if (d35.isMousePressed()) {

    d35.bringToFront();
  }
  if (d36.isMousePressed()) {

    d36.bringToFront();
  }
1 Like

forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays.html

2 Likes

Thank you. But I don’t understand how I am going to make an array with d1, d2, d3, etc. They are not “int”

Just replace int by the desired datatype: final DataType[] dArray = new DataType[37];

Ok, thanks…
Do I have to do something like DataType[] = {d1,d2,d3…d36} ??
Sorry, I am a real beginner with Processing. Arduino seems to be quite different in many aspects. I am a beginner with arduino as well…

docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Thank you. will read it and try to find examples… I just need to understand the logic of it.

Hi jhsa,

Indeed like GoToLoop is saying, an array is nothing but a list, and can be of any one datatype. But like you remarked, you do need to define which type they are. With the example above, I suspect these are controlp5 DropDownLists, though 36 dropdowns seems pretty mad!

In any case, how you’d rewrite those as an array would be something like:

int numLists = 36;
DropdownList[] crazyListOfDropdownLists = new DropdownList[numLists];

void setup() 
{
  for (int i = 0; i < crazyListOfDropdownLists.length; i++) {
    crazyListOfDropdownLists[i] = cp5.addDropdownList("myList-d"+(i+1)).setPosition(100, 100 + i*100);
  }
}

void draw()
{
  for (int i = 0; i < crazyListOfDropdownLists.length; i++) {
    if (crazyListOfDropdownLists[i].isMousePressed()) {
      crazyListOfDropdownLists[i].bringToFront();
    }
  }
  
  
}
2 Likes

Thank you, will definitely try your suggestion, I got the feeling that it is close to what i am looking for.
You think 36 lists are crazy? I will probably need other 20 or so :smiley: :stuck_out_tongue: The “Bypass” tab has another two that aren’t visible on this one. I know I am crazy doing something like this as my first, well, second project. the first one was reading/saving the eeprom from/to my DIY guitar effects switcher.
Have a look at what I currently have. :slight_smile: