g4p: Send Text if a button is pressed

edited June 2015 in Library Questions

Hi dudes! I'm a new user of this forum and i hope to help you in the future. But now I need an help...or i cannot continue my Processing project :P

I download the g4p library and my aim is to create a simple program: once insterted the text into the text area if you push a button you could store this text in a String/Array.

I thought it was easy but I don't understand why i cannot insert in function like void handleButtonEvents (GButton button, GEvent event) a new (GTextArea) argoument, because if I insert this, the function stop working.

Do you know how to solve this problem anyhow?

P.S. Sorry for my bad english :(

Answers

  • edited June 2015 Answer ✓

    void handleButtonEvents (GButton button, GEvent event)

    is called the method signature. This one defines a method called handleButtonEvents that requires two parameters, a GButton and a GEvent in that order.

    When G4P detects a click on a button it will look for a method with this signature so if you simply add another parameter then you are changing the signature and G4P will not find the function.

    Here is a simple demo of how to do what you want. The code should be self explanatory.

    import g4p_controls.*;

    GButton btn;
    GTextArea txa;
    String[] textArray = new String[0];
    String myText = "";
    
    void setup() {
      size(400, 200);
      btn = new GButton(this, 210, 20, 180, 30, "Get Text");
      txa = new GTextArea(this, 10, 20, 180, 170, G4P.SCROLLBARS_BOTH);
      textSize(10);
      fill(0);
    }
    
    void draw() {
      background(250);
      text(myText, 210, 60, 180, 140);
    }
    
    void handleButtonEvents(GButton button, GEvent event) {
      // If we have more than one button then this is how we
      // check which one was cllicked. 
      if (button == btn && event == GEvent.CLICKED) {
        textArray = txa.getTextAsArray();
        myText = join(textArray, "\n");
        println("XXX" + myText + "XXX");
      }
    }
    
  • My mistake was to contract GTextArea txa = new GTextArea(this, 10, 20, 180, 170, G4P.SCROLLBARS_BOTH); into the setup.

    Thank you so much! :D

Sign In or Register to comment.