SpringGUI
download reference examples faq
Introduction and guide
SpringGUI is a GUI manager that allows you to create user interface elements in your Processing applications and respond to events that they send to your programs whenever your user does his user-things. In terms of Java, SpringGUI wraps around the java.awt Components, and it comes in two flavours: a beginner mode that gives your program the information it needs about events, and an advanced mode that provides access to the underlying awt classes if you find that you need it.
Creating user interfaces with SpringGUI is quite simple: You create an instance of SpringGUI with which you can add GUI elements to your applet by calling methods such as addButton or addTextField . By giving these user interface elements unique names, you can later on refer to them and query or change their properties by calling methods like getText or setEnabled . GUI elements send events (tech lingo for what happens when the user presses a button, holds down a key on the keyboard or drags the mouse cursor over a TextArea) to your Processing application through an handleEvent(String[] parameters) method that you must specify somewhere in your program:
handleEvent takes an array of Strings as an argument, in which information about the events is stored. The content and length of the array will vary, depending on the type of GUI element and event, but generally the first String always contains the type of element (such as "Button" or "TextField"), the second String always contains the name of the element that sent the event, and the third String describes the type of event that happened (such as "keyReleased" or "mousePressed"). Therefore, you can handle events from within your handleEvent method with code like:
if ( parameters[1].equals("myButton") && parameters[2].equals("mousePressed") ) {
// the button named "myButton" has been pressed down...
}
Two important things to note here are that in Processing, arrays start at index 0, so parameters[1] actually refers to the second parameter; and that Strings cannot be compared directly (by saying String1 == String2 ) but have to be compared by String1.equals(String2) .
Depending on the type of event, the parameter array may contain additional entries, such as a String representation of a key pressed by the user when a "keyPressed" event is fired. These entries are described in detail in the reference for the user interface elements and events they keep track of.
If you are an experienced coder and have a bit of background in Java, you can alternatively implement the method
void handleEvent(Object[] parameters) instead of
void handleEvent(String[] parameters)
If SpringGUI detects that your Processing application specifies handleEvent(Object[] parameters) , it uses this method instead of the one taking the String[] argument (whether you implement it as well or not) and switches to advanced mode. In advanced mode, SpringGUI sends parameters of the events in form of an Object array instead of Strings; however all but the last two Objects in the array are the same as in beginner mode and can be cast back to Strings to be parsed as described:
if ( ((String) parameters[1]).equals("myButton") && ((String) parameters[2]).equals("mousePressed") ) {
// the button named "myButton" has been pressed down... in advanced mode, you have to cast the parameters to Strings explicitly.
}
The last two Objects in advanced mode are always references to underlying awt objects - for example, in a "mousePressed" event, parameter 4 contains a reference to the Component that fired the event (which can then for instance be cast into a java.awt.Component or java.awt.Button ) and parameter 5 (the last parameter) references the MouseEvent that took place.
In summary, in advanced mode, the parameter array is two entries longer than in beginner mode and the last two objects in the array can be used to gain access to advanced methods awt objects expose, or methods that currently have no equivalent part in SpringGUI. The difference between beginner mode and advanced mode for all other parameters is that the standard parameters must be explicitly cast to Strings in advanced mode.
back to the top of the page
|