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);
}
}