How to register EventBus only once

Jahongir Sabirov :

I'm writing JavaFX application in order to send from one controller to other controller. I use EventBus which was written by developer. I download it from github.But When I try to recall from one controller to other controller. First time it works once. Second time it works twice. Third time it works three times and so on. What might be reason of behaving this eventbus?

MainController

here Event bus was registered like static

public class Main extends Application
{
    public static EventBus eventBus = new FxEventBus();

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}

This controller class which was fired event

Controller

private    AddClickedEvent addClickedEvent;
    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

        id.setOnAction(event ->
        {
            try
            {
                FXMLLoader loader = new FXMLLoader();
                Parent parent = loader.load(getClass().getResource("ask.fxml").openStream());
                Stage stage = new Stage();
                stage.setScene(new Scene(parent));
                if(addClickedEvent == null){
                    addClickedEvent = new AddClickedEvent(AddClickedEvent.ANY);
                }
                Main.eventBus.fireEvent(addClickedEvent);
                stage.showAndWait();
            } catch (IOException e) {
                e.printStackTrace();
            }

        });

    }

Here is Controller other Controller that should show up something after fire

  @Override
    public void initialize(URL location, ResourceBundle resources)
    {

        Main.eventBus.addEventHandler(AddClickedEvent.ANY,event -> {
            System.out.println("uyondan bosilib galdi");
            System.out.println(yes);
            yes = true;
        });
        id1.setOnAction(event -> {
            System.out.println(yes);
        });
        id2.setOnAction(event -> {

            Stage stage = (Stage)((Node) (event).getSource()).getScene().getWindow();
            stage.close();
        });

    }

AddClicked Event class

public class AddClickedEvent extends Event
{
    public static final EventType<AddClickedEvent> ANY =
            new EventType<>(Event.ANY, "ADD_CLIENT_EVENT");

    public AddClickedEvent(@NamedArg("eventType") EventType<? extends Event> eventType) {
        super(eventType);
    }

}
Jahongir Sabirov :

EventHandler should be created only once in process of whole application then it will react only once rather than multiple times. I come up with solutions to it declare for every controller class with static int variable which helps me to register events only once when the value of int is zero.

      private static int check;
      @Override
        public void initialize(URL location, ResourceBundle resources)
        {
          if(check == 0)
{

            Main.eventBus.addEventHandler(AddClickedEvent.ANY,event -> {
                System.out.println("uyondan bosilib galdi");
                System.out.println(yes);
                yes = true;
            });
check ++;
// Then it will react only once We registered event here
}
            id1.setOnAction(event -> {
                System.out.println(yes);
            });
            id2.setOnAction(event -> {

                Stage stage = (Stage)((Node) (event).getSource()).getScene().getWindow();
                stage.close();
            });

        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=70532&siteId=1