0

I am following this tutorial http://java.dzone.com/articles/fxml-javafx-powered-cdi-jboss for the moment the @Inject works perfectly with JavaFX. I would like to make full advantage of CDI-Weld using CDI Events and placing @Observers in the controllers methods, so I could fire events from the model to manipulate the Controllers with no coupling:

The Quesion is this one: The CDI Event is fired properly and the payload reach destination, but all the @FXML's objects are null when I need to use them inside the event Method. it is like, I am observing to other place where the @FXML injection did initialize the fields.

I am not sure where to put my efforts if develop a portable extension or there is an easier way around?

package com.codexven.weldtest.javafx;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application.Parameters;
import javafx.event.ActionEvent;
import javafx.fxml.*;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import javax.enterprise.event.Observes;

import javax.inject.Inject;

`public class LoginController implements Initializable {
    // Standard FXML injected fields
    @FXML TextField loginField;
    @FXML PasswordField passwordField;
    @FXML Text feedback;

    @Inject SimpleLoginService loginService;

    @Inject Parameters applicationParameters;

    @FXML protected void handleSubmitButtonAction(ActionEvent event) {
        feedback.setText(loginService.login(loginField.getText(), passwordField.getText()));
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
            System.out.println("ls "+loginService);
        loginField.setText(applicationParameters.getNamed().get("user"));
    }

        public void eventTest(@Observes SimpleLoginService service){
            System.out.println("ls "+loginService);
        loginField.setText("Hello Event");
    }
}

And in the SimpleLoginService:

package com.codexven.weldtest.javafx;

import com.codexven.weldtest.weld.LoggedIn;
import javax.enterprise.event.Event;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;

public class SimpleLoginService implements LoginService {

        @Inject
    @LoggedIn
    Event<SimpleLoginService> authenticateEvent;


    @Override
    public String login(String login, String password) {
        if (password != null && password.trim().length() > 0) {

                authenticateEvent.select(new AnnotationLiteral<LoggedIn>(){}).fire(this);
            return String.format("%1$s logged in successfully", login);
        }
                authenticateEvent.select(new AnnotationLiteral<LoggedIn>(){}).fire(this);
        return String.format("%1$s failed to login", login);
    }
}
LuCG
  • 109
  • 9
  • Have you researched on what is @FXML and how it is used? – Uluk Biy Jun 02 '15 at 04:14
  • yes, just near to find the solution. The main problem of my approach is that: authenticateEvent.select(new AnnotationLiteral(){}).fire(this); is creating a new instance of the controller every time I called it (it dynamically instantiate and use the method of the object). So this approach is not the best one. I will try to get to a better solution. – LuCG Jun 02 '15 at 05:41
  • "When Weld is in the process of delivering events to observers during our call to Event.fire(), it will automatically instantiate the bean that defines the observer method to be called if there isn't currently an in instance of that bean present within the current context." [JBoss Weld CDI for Java Platform] So the problem seems to be that the Controller is not in the current context and weld is creating a new instance of it. – LuCG Jun 03 '15 at 15:37
  • Maybe but the main concern here, is that the @FXML annotated fields are instantiated by FXMLLoader. So to be injected those fields you need to implement some mechanism for Weld to use fxmlloader.load(). – Uluk Biy Jun 04 '15 at 04:58
  • 1
    Have you found any solution for this problem? I'm struggling with it for a while. Can you provide a final solution if any was discovered? Or maybe a walkaround? :) – Bartek Mar 20 '17 at 12:22

0 Answers0