If you recently sent an email to me about this topic then I have responded. Just in case you are someone else or you are trying something similar then this is my answer ---
When using G4P there are 2 things that need to be considered
- creation of the components and
- handling of the events.
Most users will do both these inside the main sketch window and in this case when Processing runs the program it wraps up your code inside a class (same name as the skecth) then send the result to javac to compile and then to the jvm (Java virtual machine) which will create an object from this class and run it. So even though you have not explicitly created a class for your code one is created for you.
If you create you own class inside the main sketch window or in a seperate pde tab then the class will be considered an 'inner' class to the one created by Processing and this will not work. The solution is to create a seperate Java class. What follows is a very simple example (this code works I have tested it).
Create a sketch and add the following code
== Main sketch code ==============================
==============================
==========
- import guicomponents.*;
-
- GUI gui;
- void setup(){
- size(400,260);
- gui = new GUI(this);
- }
- void draw(){
- }
==============================
==============================
===========================
Now click on the New Tab icon in the Processing IDE and when asked for the tab name use
GUI.java
Note this is case sensitive and is the
class name followied by
.java
In this tab add the following code
== GUI.java code ==============================
==============================
==========
- import guicomponents.*;
- import processing.core.PApplet;
- public class GUI {
- PApplet app;
- GButton button1, button2;
-
- public GUI(PApplet papp){
- app = papp;
- button1 = new GButton(app, "DRINK ME", 10,30,100,20);
- button2 = new GButton(app, "EAT ME", 10,60,100,20);
- button1.addEventHandler(this, "handleMyButtonEvents");
- button2.addEventHandler(this, "handleMyButtonEvents");
- }
-
- public void handleMyButtonEvents(GButton b){
- if(b == button1){
- PApplet.println("You got smaller");
- }
- else if(b == button2){
- PApplet.println("You got bigger");
- }
- }
- }
==============================
==============================
===========================
Your button events are now hadled in the object
gui by the method called
handleMyButtonEvents
Hope this helps