SpringGUI

introduction    download    reference    examples    faq


Reference

Methods for beginners

If you are new to Processing and/or programming in general, you may feel uncomfortable working with the concept of Arrays, which is used in the way SpringGUI gives you information about what the user does with your GUI elements.

SpringGUI gives you three methods that you can use to extract information from the parameters your handleEvent method receives: getElementType(parameters) gives you the type of the element that fired an event, as a String (such as "Button" or "TextField"); getElementName(parameters) gives you the name of the element, which you specified when you created it, as a String (such as "myButton" or "TextField003"); and getEventType(parameters) gives you the type of the event, as a String (such as "mouseClicked" or "selected").

Here is a complete and working example of how to use these methods to extract information about what happened. Copy and paste this into a new Processing sketch. Don't worry about the confusing formatting here; it should look more readable within the Processing editor. If you start the sketch, a button should show up. When you press the button down, using the mouse, it will change its text to give you feedback. When you release it, it should return to its initial state.

import SpringGUI.*;

SpringGUI gui; // this will be our SpringGUI object on which all methods must be called.

void setup() { // this is called when Processing starts up
  size(200,50); // set the size of the applet.
  gui = new SpringGUI(this); // this initialises SpringGUI
  gui.addButton("myButton", "I am a button", 10,10,180,30);
}

void handleEvent(String[] parameters) { // we have to have this method or SpringGUI will not work
  String whichElement = gui.getElementName(parameters); // get the name of the element
  String whichEvent = gui.getEventType(parameters); // get the type of the event that happened
  if ( whichEvent.equals("mousePressed") && whichElement.equals("myButton") ) { // if the mouse has been pressed over "myButton" - careful with the capitalisation
    gui.setText("myButton", "I have been pressed"); // change the text on the button.
  }
  if ( whichEvent.equals("mouseReleased") && whichElement.equals("myButton") ) { // if the mouse has been released over "myButton" - careful with the capitalisation
    gui.setText("myButton", "I am a button"); // change the text on the button.
  }
}


back to the top of the page


tinkering with codetools and docsscrapbookabout