Save Value in ArrayList controlP5/Processing/Intellij

Hey Everybody, Nooby here :slight_smile:

I’m doing my first GUI and I don’t know how to save the Values in an ArrayList by pressing the Buttons

this is my Code so far:


import processing.core.PApplet;
import controlP5.*;
import processing.core.PFont;
import processing.core.PConstants;
public class ProcessingTest extends PApplet {

    ControlP5 cp5;

    int myColor = color(183, 174, 174);


    float n, n1;
    int w = 510;
    int h = 800;
    int x = 0;

    public void settings()
    {
        size(w,h);
    }


    @Override
    public void setup() {

        PFont pfont1;
PFont.list();
        pfont1 = createFont("Calvin", 40, true); // use true/false for smooth/no-smooth

        ControlFont cfont = new ControlFont(pfont1, 40);

        cp5 = new ControlP5(this);
        // create a new button with name '9' / A
        cp5.addButton("9")
                .setValue(9)
                .setPosition(260, 60)
                .setSize(100, 100);

        cp5.addButton("8")
                .setValue(8)
                .setPosition(150, 60)
                .setSize(100, 100);

        cp5.addButton("7")
                .setValue(7)
                .setPosition(40, 60)
                .setSize(100, 100);

        cp5.addButton("6")
                .setValue(6)
                .setPosition(260, 170)
                .setSize(100, 100);

        cp5.addButton("5")
                .setValue(5)
                .setPosition(150, 170)
                .setSize(100, 100);

        cp5.addButton("4")
                .setValue(4)
                .setPosition(40, 170)
                .setSize(100, 100);

        cp5.addButton("3")
                .setValue(3)
                .setPosition(260, 280)
                .setSize(100, 100);

        cp5.addButton("2")
                .setValue(2)
                .setPosition(150, 280)
                .setSize(100, 100);

        cp5.addButton("1")
                .setValue(1)
                .setPosition(40, 280)
                .setSize(100, 100);

        cp5.addButton("0")
                .setValue(0)
                .setPosition(150, 390)
                .setSize(100, 100);

        cp5.addButton("+")
                .setValue('+')
                .setPosition(370, 60)
                .setSize(100, 100);

        cp5.addButton("-")
                .setValue('-')
                .setPosition(370, 170)
                .setSize(100, 100);

        cp5.addButton("*")
                .setValue('*')
                .setPosition(370, 280)
                .setSize(100, 100);

        cp5.addButton("/")
                .setValue('/')
                .setPosition(370, 390)
                .setSize(100, 100);

        cp5.addButton("=")
                .setValue('=')
                .setPosition(260, 390)
                .setSize(100, 100);

        cp5.addButton(",")
                .setValue(',')
                .setPosition(40, 390)
                .setSize(100, 100);
        //

        //
        // change the font of the captionlabels
        // for button created earlier.
        cp5.getController("9")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("8")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("7")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("6")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("5")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("4")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("3")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("2")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("1")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("0")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("+")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("-")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("*")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("/")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController("=")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
        cp5.getController(",")
                .getCaptionLabel()
                .setFont(cfont)
                .toUpperCase(false)
                .setSize(40)
        ;
    }



    @Override
    public void draw() {
        background(54, 57, 54);
        stroke(106, 102, 100);
        fill(70, 110, 157);
    }
    public static void main (String... args) {
        ProcessingTest pt = new ProcessingTest();
        PApplet.runSketch(new String[]{"ProcessingTest"}, pt);

        controlP5.Button : String toString()
    }
}

To be more precise , it will be al calculator so if I press Buttons “1” “+” “2” = I want to Save 1 1 2
→ I allredy have a nice class to translate the INT in a mathproblem and retun the result but the conection to my gui make me struggle

Thanks alot for your help in advance

Hi! I understand working with gui/callbacks can be frustrating. Let’s start with breaking down the problems: first of all do you need to use IntelliJ? For simplicity let’s use the Processing IDE for now, and I deleted all the buttons but the first one to tidy up the code

import controlP5.*;

ControlP5 cp5;

int myColor = color(183, 174, 174);


float n, n1;
int w = 510;
int h = 800;
int x = 0;

public void settings()
{
  size(w, h);
}


public void setup() {

  PFont pfont1;
  PFont.list();
  pfont1 = createFont("Calvin", 40, true); // use true/false for smooth/no-smooth

  ControlFont cfont = new ControlFont(pfont1, 40);

  cp5 = new ControlP5(this);
  // create a new button with name '9' / A
  cp5.addButton("9")
    .setValue(9)
    .setPosition(260, 60)
    .setSize(100, 100);

  //

  //
  // change the font of the captionlabels
  // for button created earlier.
  cp5.getController("9")
    .getCaptionLabel()
    .setFont(cfont)
    .toUpperCase(false)
    .setSize(40)
    ;
}



public void draw() {
  background(54, 57, 54);
  stroke(106, 102, 100);
  fill(70, 110, 157);
}

now what you need is not going straight to ArrayList, but somehow you need to read the value on button press. For example, to write the value to the console. Actually controlP5Button example already has the answer:

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.getController().getName());
}

So now it’s your task to use this controller name to add it to a list :slight_smile: