How to create custom signal in java-gnome?

Viewed 214

I am trying to create custom signal in java-gnome in my GTK+3 application but I can't.

import org.gnome.gtk.CheckButton;

public class MyCheckButton extends CheckButton {

    // how can I add a cutom signal for this class
    // fos example: the signal that has "my-signal" name.
}

How can I do it?

I can do it in python

class MyCheckButton(Gtk.CheckButton):
    __gsignals__ = {
        'my_signal': (GObject.SIGNAL_RUN_FIRST, None,
                      (int,))}
abc = MyCheckButton()
abc.connect("my-signal", print)
abc.emit("my-signal", 543)

I've searched on the internet but I couldn't find anything.

Thanks.

1 Answers

I think there is no similar way in Java (based on the fact that there is no similar structure in the source code).

But you can implement your own handler by:

  • Define the handler interface (#1)
  • Add a connect method (#2, #3)
  • Add an emit method (#4)

Sample code:

public class MyCheckButton extends CheckButton {

    // ...

    private MySignalHandler handler; // #2

    public interface MySignalHandler {  // #1
        public void onMySignal(int value);
    }

    public void connect(MySignalHandler handler) {  // #3
        this.handler = handler;
    }

    public void emitMySignal(int value) {  // #4
        if (handler == null) return;
        handler.onMySignal(value);
    }

}

And then you can connect and emit in the same way as the other events.

MyCheckButton a = new MyCheckButton();

a.connect(new MyCheckButton.MySignalHandler() {
  public void onMySignal(int value) {
    System.out.println(value);
  }
});

a.emitMySignal(10);

The built-in Button uses something similar to define the click event by adding the connect(Clicked handler) and emitClicked() (source code)

Related