[SOLVED] .hide() not working for buttons

Hello I just started using Processing. I intend to have this code such that when the buttons are pressed, they disappear. However when I click, the buttons remain on screen. How do I fix this?

Pardon my unusual way of commenting

import controlP5.*;
import processing.serial.*;

//Serial port = new Serial(this, "COM4", 9600); //COMport and baud rate
ControlP5 cp5_A;
ControlP5 cp5_B;

PFont buttonfont;
PFont optionfont;

//\\ Main setup
void setup() {
  
  //\\ Primary customisation
  size(640, 300);
  background(249, 250, 255);
  textSize(24);
  smooth();
  fill(0);
  text("Control Centre", 10, 30);
  
  //\\ Fonts
  buttonfont = createFont("Arial", 18, true);
  optionfont = createFont("Arial", 12, true);
  
  //\\ Initialise controller layers and relevant buttons
  cp5_A = new ControlP5(this);
  
    cp5_A.addButton("single")
    .setPosition(170, 110)
    .setSize(100, 60)
    .setFont(buttonfont)
    .setId(1);
  
    cp5_A.addButton("swarm")
    .setPosition(370, 110)
    .setSize(100, 60)
    .setFont(buttonfont)
    .setId(2);
  
    cp5_A.addButton("manual")
    .setPosition(540, 0)
    .setSize(100, 60)
    .setFont(buttonfont)
    .setId(3);
  
    cp5_A.addButton("options")
    .setPosition(0, 270)
    .setSize(80, 30)
    .setFont(optionfont)
    .setId(4);
  
  cp5_B = new ControlP5(this);
  
}

//\\ Functions
public void addHome(){
  cp5_B.addButton("Home")
  .setPosition(580, 270)
  .setSize(60, 30)
  .setFont(optionfont);
}

public void clearPage(){
  //clears the page so as to make space for new controllers (except title)
  /*cp5_A.remove("single");
  cp5_A.remove("swarm");
  cp5_A.remove("options");
  cp5_A.remove("manual");*/
  cp5_A.hide();

}

public void single(){
  println("single option");
  
}

public void swarm(){
}

public void options(){
}

public void home(){
  //
}

//\\ Actual loop
void draw(){
  
  if(cp5_A.isMouseOver(cp5_A.getController("single")) & mousePressed){
    addHome();
    clearPage();
    single();
  }
  
  else if (cp5_A.isMouseOver(cp5_A.getController("swarm")) & mousePressed){
    clearPage();
    addHome();
    swarm();
  }
  
  else if (cp5_A.isMouseOver(cp5_A.getController("options")) & mousePressed){
    clearPage();
    addHome();
    options();
  }
  
  else if (cp5_B.isMouseOver(cp5_B.getController("Home")) & mousePressed){
    clearPage();
    home();
    println("aaaaa");
  }
  
 
}
1 Like

cp5_A
is not a button, what give you the idea that the .hide() method can be used on that?
try on the single buttons or on a group of buttons.

1 Like

Hello! Hmm let me try to explain: cp5_A is simply the ‘layer’ i associated a few buttons to. Correct me if I am wrong but it is no different from cp5.hide();?

It would not work on single buttons, like for example, “single”.hide();

here it works with that example code
made from the button example

/**
 * ControlP5 Button
 * by Andreas Schlegel, 2012
 * www.sojamo.de/libraries/controlp5
*/
// kll test HIDE
// https://discourse.processing.org/t/hide-not-working-for-buttons/11962

import controlP5.*;

ControlP5 cp5;
Button bA,bB,bC;

void setup() {
  size(300,300);
  noStroke();
  cp5 = new ControlP5(this);

  bA = cp5.addButton("A")
     .setValue(0)
     .setPosition(100,100)
     .setSize(100,19)
     .setLabel("SHOW C")
     ;
  bB = cp5.addButton("B")
     .setValue(1)
     .setPosition(100,150)
     .setSize(100,19)
     .setLabel("HIDE C")
     ;
  bC = cp5.addButton("C")
     .setValue(2)
     .setPosition(100,200)
     .setSize(100,19)
     .setLabel("see this?")
     ;
}

void draw() {
  background(200,200,0);
}

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

// controller with name A
public void A(int theValue) {
  println("a button event from A: "+theValue);
  bC.show();
}

// controller with name B
public void B(int theValue) {
  println("a button event from B: "+theValue);
  bC.hide();
}


1 Like

Thank you for your help @kll, got it to work in the end. :slight_smile: