Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Singletons and communication between controllers

984925Jan 15 2013
Hello,
recently i've made a few javafx applications and I faced issues with communication between controllers and stages. I've made a small framework that allows me to create singleton controller/stage and an event mechanism for communication between controllers. I'm really eager to know yours opinions about my framework so I decided to publish it. Source are available on https://github.com/lobo1111/fx-framework.
I'm posting a small manual below:

- All controllers have to extend SceneController class and have an annotation:
@FXMLController(fxmlPath = "/fxframeworktest/beans/First.fxml", singleton = false)
public class FirstController extends SceneController {
}
initialize method is replaced by onInit(). You can create controller by calling:
SceneController controller = ControllersManager.getInstance().getController(FirstController.class);
If controller is a singleton, it will be created only once.

- To make a Stage, you can use StagesManager class:
Stage newStage = StagesManager.getInstance().getStage(controller);
If controller is a singleton, stage will be created only once.
Every controller has a method close(), it will dispose controller and stage(if available), singleton controller won't be disposed and it's stage will be hide.
If controller has a child controllers inside and there are injected like this:
@FXMLController(fxmlPath = "/fxframeworktest/beans/Third.fxml", singleton = false)
public class ThirdController extends SceneController {

    @FXML
    private ChildOfThird childController;

}
there also be disposed, but of course only if main controller is not a singleton.

- For communication betweent controllers there is a method:
controller.fireEvent(SceneEvent event)
All initialized controllers will be noticed about fired event and they can catch it:
public class TestEvent extends SceneEvent {
}

...

@FXMLController(fxmlPath = "/fxframeworktest/beans/First.fxml", singleton = false)
public class FirstController extends SceneController {

    @Override
    protected void onInit() {
        fireEvent(new TestEvent());
    }
}

...

@FXMLController(fxmlPath = "/fxframeworktest/beans/Second.fxml", singleton = false)
public class SecondController extends SceneController {

        @SceneEventHandler
        public void eventHandlerMethod(TestEvent event) {

        }
    }
}
Event will be catch if method has SceneEventHandler annotation and only one parameter. Parameter class has to be accessible from event class.

That's pretty match all. Feel free to use and if you like this, please give me a feedback :)

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 12 2013
Added on Jan 15 2013
0 comments
2,429 views