In G4P there are two techniques for handling events, they are ‘by control type’ and ‘by control’
- By Control Type
This was the original technique that was used in the first release and is still remains available. In this technique when the sketch creates a button e.g. btn = new GButton(this, ...);
G4P will look for a method called handleButtonEvents
that has two parameters a GButton reference and a GEvent reference if it can’t find this method then it will display a message in the console tab -
You might want to add a method to handle GButton events syntax is
public void handleButtonEvents(GButton button, GEvent event) { /* code */ }
BTW this method is called an event handler.
This is great because it shows the correct method signature and the user can always copy and paste the second line into the source code. I always found this useful because I couldn’t remember the method signatures
So if we had a sketch with 5 buttons this event handler will process all events fired by them. this requires a series of if
statements inside the method to decide which button was firing the event. By default buttons only fire events of type GEvent.CLICKED so it is not necessary to test the event type.
If a button executes the method fireAllEvents(true);
then that button can fire three types of event
GEvent.CLICKED, GEvent.PRESSED and GEvent.RELEASED
so it would be necessary to differentiate the event type using if
or switch
statements.
When a button is clicked it fires two events GEvent.PRESSED
followed by GEvent.CLICKED
and each one would result in the event handler being executed separately i.e. the event handler is called twice
Other control types have different event handlers e.g.
// Text fields and ares
public void handleTextEvents(GEditableTextControl textcontrol, GEvent event) { /* code */ }
// Sliders and custom sliders
public void handleSliderEvents(GValueControl slider, GEvent event) { /* code */ }
These control types already fire multiple event types.
#---------------------------------------------------------------------------------------------------#
- By Individual Control
This was introduced later when GUI Builder became available. Now each control can have its own event handler so in the statement
btnFireEvent.addEventHandler(this, "processButtonEvents");
there is a GButton called btnFireEvent
and the event handler signature will be
public void processButtonEvents(GButton source, GEvent event) { }
NOTE:
In a previous post you had
btnFireEvent.addEventHandler(this, "handleButtonEvents");
the sketch did not recognize bthFireEvents
because no such button has been created. Also I recommend that you do not use method names used by technique 1, it will lead to inconsistent behaviour.
Having a unique event handler for each control is the much preferred technique because it simplifies the code in each event handler.
#---------------------------------------------------------------------------------------------------#
Both techniques are still available and can be used in the same sketch that is why it is important that event handlers for technique 2 have different names to the default names used in technique 1.
Last point for the moment - the syntax foe testing the event type is slightly different between if
and switch
statements e.g.
if(button == imgBtnPrev && event == GEvent.RELEASED){
...
}
// Need the 'GEvent.'
versus
switch(event) {
case CLICKED:
txaOutput.appendText("Button event: GEvent.CLICKED @ " + millis());
break;
case PRESSED:
txaOutput.appendText("Button event: GEvent.PRESSED @ " + millis());
break;
case RELEASED:
txaOutput.appendText("Button event: GEvent.RELEASED @ " + millis());
break;
}
// 'Gevent.' not required