public static interface Button.Clicked
When the mouse is used to click on a Button this signal will be emitted, but only if the cursor is still in the Button when the mouse button is released. You're probably used to this behaviour without realizing it.
In general, the way that java-gnome is intended to be used is for you
to create an anonymous inner class right where you call the
connect()
method to hook up the signal. A typical example
is as follows:
final Button b; b.connect(new Button.Clicked() { public void onClicked(Button source) { doSomething(); } }This is the form we recommend; it has the advantage that your handler code is close to the code that declares and configures the Widget. Also, if variables are declared
final
then you can access
them from within the nested anonymous class, and that makes things easy
indeed.
If you have more complex code, perhaps that you need to reuse across several Widgets, then you can of course create a concrete class that implements Button.Clicked and then use instances of it if you have highly complicated algorithms to implement:
class ComplexHandler implements Button.Clicked { private int field; ComplexHandler(int param) { this.field = param; } public void onClicked(Button source) { doSomethingVeryComplicatedWith(field); } }and then when you construct the Button:
b = new Button(); b.connect(new ComplexHandler(42));You can just as easily declare one and reuse it if you want to apply the same logic to a series of Button; this is where the
source
parameter that every signal callback signature
includes.
final ComplexHandler handler; handler = new ComplexHandler(42); ... b1.connect(handler); b2.connect(handler); b3.connect(handler); ...
If you implement Button.Clicked in the class you're currently working on, then you use a technique called "self-delegation" which can be useful in certain circumstances:
b.connect(this);This has disadvantage that you can only have one
onClicked()
method in your class, so if you have lots of
Buttons then you lose the benefit of being able to connect different
handlers to different Buttons.
We recommend you use anonymous inner classes in-place whenever possible. In our experience, having the handler logic next to the code that declares an instance dramatically improves readability.
void onClicked(Button source)