1

This question is so obvious, and I am sure that it is so easy to solve that I wonder why no simple and easy to understand answer hes been posted yet. I’ve found answers explaining how to create a *new event, changing the event direction, -bubble, etc. However, I have not found a Q/A regarding catch a signal and then bounce it off to the user class, and then to a class that the is user of that user class and so on and so forth. Having said that, here is my question.

My class, ClassOne, receive the signal of a button and then has to pass that signal to ClassTwo, which in turn passes the signal to ClassThree, how that heck do I do this in JavaFx.

Please note that the class must not fire a new event, but the same event that has been caught.

Below is a grammatical explanation of what I am trying to do.

Any help, and I mean any input would be most appreciated.

public class MyBoxedButton extends HBox {
  private Button btn;

  MyBoxedButton(){
    // Initialization of all the objects
    btn = new Button();
    //catch the event emitted by the button 
    btn.setOnAction((ActionEvent e) ->{

        // fire/emit the same received event
        ?????
    });
  }
}

/*
This class catches the event emitted by MyBoxedButton and then passes it to ClassThree, 
but this does not work, how then can I catch the event and then re-emit it.
*/
class ClassTwo(){
    private MyBoxedButton mbb;

    public ClassTwo(){
        mbb = new MyBoxedButton();
        //catch the event emitted by MyBoxedButton
        ????
        // re-emit the caught event
        ????
    }
}   

/*
This class catches the event emitted by ClassTwo.
The exersice is ment to show how the messages flow 
form one object to a next and then to a next, so on and so forth.
*/
class ClassThree(){
    private ClassTwo c2;

    public ClassThree(){
        c2 = new ClassTwo();
        //catch the event emitted by MyBoxedButton
        ????
        // re-emit the caught event
        ????
}   
Puce
  • 37,247
  • 13
  • 80
  • 152
Papa
  • 41
  • 3
  • There is no `ClassOne` class in your post. You try to declare a method in the constructor of `MyBoxedButton`. There is a constructor in `ClassThree` named `ClassTwo`. If `ClassTwo` and `ClassThree` are indeed supposed to be non-static inner classes of `MyBoxedButton`, this is very odd design: You'd need to create a instance of `MyBoxedButton` to create a instance of `ClassThree` which in turn would create a new instance of `MyBoxedButton` when creating a `ClassTwo` instance. – fabian Dec 26 '18 at 18:18

1 Answers1

0

As long as your wrapper classes also extend Node, fireEvent should do the trick, I think.

MyBoxedButton and ClassTwo will have to provide a way to register a listener.

I've written the SimpleEventHandlerProperty helper class, which makes this easier. Here is a code snippet from a sample class which uses SimpleEventHandlerProperty to allow to register an ActionEvent handler.

private final ObjectProperty<EventHandler<ActionEvent>> onNewSampleAction = new SimpleEventHandlerProperty<>(this,
        "onNewSampleAction", ActionEvent.ACTION, this::setEventHandler);


public final EventHandler<ActionEvent> getOnNewSampleAction() {
    return onNewSampleActionProperty().get();
}

public final void setOnNewSampleAction(EventHandler<ActionEvent> contentChanged) {
    onNewSampleActionProperty().set(contentChanged);
}

public ObjectProperty<EventHandler<ActionEvent>> onNewSampleActionProperty() {
    return onNewSampleAction;
}

Then ClassTwo and ClassThree can register a listener as required.

The helper class is available at Maven Central:

<dependency>
    <groupId>org.drombler.commons</groupId>
    <artifactId>drombler-commons-fx-core</artifactId>
    <version>0.13</version>
</dependency>
Puce
  • 37,247
  • 13
  • 80
  • 152