Javafx MVC how to signal new threaded result from one controller to another - javafx

I have a Javafx MVC Structure where Controller 1, 2, 3... are in charge of updating the UI elementes, and Super Controller executes whatever job is passed to it from them, and returns the result.
I have a Button Btn1 in Controller1 which when pressed will be disabled and show a processing symbol while the main job is done in other thread whose function is present in Super Controller. Once that thread completes its job, it'll notify the calling function that job is done, so that Btn1 will be enabled again, and processing symbol removed.
CONTROLLER1
#FXML
Button Btn1;
private ProgressIndicator loading;
private SUPERCONTROLLER con;
#Override
public void initialize(URL url, ResourceBundle rb){
loading=new javafx.scene.control.ProgressIndicator();
loading.setProgress(javafx.scene.control.ProgressIndicator.INDETERMINATE_PROGRESS);
}
#FXML
private void press(MouseEvent evt){
Btn1.setDisable(true);
Btn1.setGraphic(loading);
Task<Integer>t=this.con.Action();
new Thread(t).start();
t.setOnSuceeded(e->{
if(t.getValue()==0){
System.out.println("Success");
}else{
System.out.println("falure");
}
Btn1.setDisable(false);
Btn1.setGraphic(null);
});
}
private void inject(SUPERCONTROLLER c){
this.con=c;
}
SUPERCONTROLLER
#FXML
private VBox sidePane;
#Override
public void initialize(URL url,ResourceBundle rb){
var fxml=new FXMLLoader(getClass().getResource("CONTROLLER1.fxml");
Pane childPanel=fxml.load();
sidePane.getChildren().add(childPanel);
CONTROLLER1 childPanel.getController();
CONTROLLER1.inject(this);
}
protected Task<Integer> Action(){
Task<Integer>task=new Task<>(){
#Override protected Integer call(){
Thread.currentThread.wait(3000); //Imitate long task
return 0;
}
}
return task;
}
However, I want the thread concept to stay in Super Controller itself. i.e., Instead of setting suceeded operation in Controller1, I want to move it to Super Controller and Controller1 should just re-enable the button when Super Controller tells it.

Related

JavaFX - parsing child arguments back to parent

I have 2 Controllers and I want to pass values from second controller to first controller, here are code sample:
FXMLController.java
private int paramAnswers;
private int paramNotificationTime;
private int paramNotificationDelay;
#FXML
private void handleMenuItemOptionsAction(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Options.fxml")); // UNDECORATED*
Scene scene = new Scene(root, Color.TRANSPARENT);
final Stage stage = new Stage();
stage.setTitle("Options");
stage.setScene(scene);
stage.show();
}
public void setOptionsParams(int paramAnswers, int paramNotificationTime, int paramNotificationDelay){
this.paramAnswers = paramAnswers;
this.paramNotificationTime = paramNotificationTime;
this.paramNotificationDelay = paramNotificationDelay;
}
and second controller:
OptionsController.java
private FXMLController parentController;
private int paramAnswers;
private int paramNotificationTime;
private int paramNotificationDelay;
#Override
public void initialize(URL location, ResourceBundle resources) {
.... }
#FXML
private void handleButtonSaveAction(ActionEvent event) throws IOException{
/*Pass these parameteres OptionsController parameters back to the FXMLController like parentController.setOptionsParams(paramAnswers, paramNotificationTime, paramNotificationDelay);
*/
(((Button)event.getSource()).getScene().getWindow())).close();
}
Arleady tried with parsing FXMLControler as .this into OptionsController initialize method, tried making listers and bunch of other resolved problems on stackoverflow but it just don't want work :< I need to pass that atributes back to FXMLController and close child window, so my main app would change behavior depending on passed values... :X
For any help I will be grateful
you can have a function in the second controller let's say passParams() set it's parameters what every you want to pass to that controller and from the first controller when you click on a button or something
this line
FXMLLoader.load(getClass().getResource("/fxml/Options.fxml"));
need to be changed to
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Options.fxml"));
Parent root = (Parent) loader.load();
OptionsController controller = loader.getController();
EDIT
you need to pass the first controller to the second controller
controller.setParentController(this); // this is the first controller
in Second controller
FirstController mController;
public void setParams(FirstController controller) {
this.mController = controller;
}
now in the button click function you use the mController you got from the previous step
mController.setOptionsParams(...); //send the params collected from the textfields
this function is implemented in the FirstController
Note: a more general way to do this by using call-backs it's the same but your code depends on interfaces not concrete classes by implementing a general interface in FirstController that have the setOptionParams() method
First you add a callback interface to your parent controller class:
public interface OptionCallback {
public void setOptionsParams(int paramAnswers, int paramNotificationTime, int paramNotificationDelay);
}
public class YourParentController implements Initializable, OptionCallback {
...
}
Using the FXMLLoader object you can get a hold of your child controller object and pass the parent object to it:
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("yourFXML.fxml"));
Parent root = (Parent) loader.load();
YourChildController yourChildController = loader.<YourChildController>getController();
yourChildConrtoller.registerCallback(this);
In the childController you save that callback:
private OptionCallback optionCallback;
public void registerCallback(OptionCallback callback) {
optionCallback = callback;
}
And whenever results are ready you use it to pass it to parent:
optionCallback.setOptionsParams(...);

mvvmFX: Changing Part of a Scene on Runtime

I'm building a JavaFX Application with mvvmFX.
I have a scene with two panes. Both panes should include any.fxml . I would like to change the included panes at runtime.
So I've found this solution: JavaFX/SceneBuilder - Changing only PART of a Scene and tried to apply it.
So far, so good. Pane2 is displaying any.fxml, but unfortunately, the controller/viewModel is not getting loaded. At least it seems so, any.fxml contains a label which should get modified by the viewModel.
If O add fx:controller="anyVM" to any.fxml and include it via fx:include source="any.fxml" in pane2.fxml everything is working fine. But it's important for me to change it on runtime.
I would be glad if somebody knows a solution how to achieve this.
This is the View of my Main-Scene:
public class Scene implements FxmlView<SceneVM>, Initializable {
#FXML
private BorderPane pane2;
#InjectViewModel
private SceneVM sceneVM;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("any.fxml"));
loader.setController(anyVM.class);
try {
pane2.setCenter(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is pane2 in scene.fxml:
<BorderPane fx:id="pane2" layoutX="846.0" prefHeight="547.0" prefWidth="521.0">
<center> <!--<fx:include source="any.fxml" />-->
</center>
</BorderPane>
This is the ViewModel / Controller
#Singleton
public class anyVM implements ViewModel {
private StringProperty lbl = new SimpleStringProperty("27");
public StringProperty getLbl(){
return basementViewKesselTempLbl;
}
public void setLbl(String message){
lbl.set(message);
}
}
In your example code of initialize you are using FXMLLoader which is part of standard JavaFX. However, to load a mvvmFX View with all setup logic you have to use mvvmFX's FluentViewLoader.
The FluentViewLoader uses FXMLLoader internally but does a whole lot more operations like injecting ViewModels into their Views.
So your code should look like this:
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ViewTuple<AnyView, AnyViewModel> viewTuple = FluentViewLoader.fxmlView(AnyView.class).load();
pane2.setCenter(viewTuple.getView());
}

How to prepare FXML Controller to be GC

I have an issue in my application. Any Controllers are garbage collected.
I was prepared one, pretty simple with clear function, but it is no removed from memory.
#Log4j2
public class DialogOpenProjectController extends DialogPane implements Initializable, FXMLController, FXMLPane, FXMLDialogController {
#FXML
private ObjectProperty<ResourceBundle> languageBundle = new SimpleObjectProperty<>();
#FXML
private JFXTabPane TAB_PANE_OPEN_PROJECT;
#FXML
private JFXButton BUTTON_CONFIRM;
private Tab tabOpenNewProject;
private Tab tabOpenExistingProject;
private Stage stage;
private ChangeListener<? super ResourceBundle> languageListener = this::languageChange;
private ChangeListener<? super Tab> selectedTabListener = this::selectedTabChanged;
{
tabOpenExistingProject = new Tab();
tabOpenNewProject = new Tab();
}
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
log.trace(LogMessages.MSG_CTRL_INITIALIZATION);
this.languageBundle.setValue(rb);
initTabs();
addSelectedTabListener();
addDisableButtonListener();
addLanguageListener();
log.trace(LogMessages.MSG_CTRL_INITIALIZED);
}
#FXML
public void cancel() {
this.stage.close();
clean();
}
#FXML
public void confirm() {
if (TAB_PANE_OPEN_PROJECT.getSelectionModel().getSelectedItem().equals(tabOpenNewProject)) {
actionNewProject();
} else if (TAB_PANE_OPEN_PROJECT.getSelectionModel().getSelectedItem().equals(tabOpenExistingProject)) {
actionOpenProject();
} else {
//To to show error
}
this.stage.close();
clean();
}
private void initTabs() {
TAB_PANE_OPEN_PROJECT.getSelectionModel().select(tabOpenNewProject);
}
private void addSelectedTabListener() {
TAB_PANE_OPEN_PROJECT.getSelectionModel().selectedItemProperty().addListener(selectedTabListener);
}
private void addDisableButtonListener() {
//nothing to do temporary
}
private void clean() {
this.languageBundle.removeListener(languageListener);
languageBundle.unbind();
languageBundle.setValue(null);
TAB_PANE_OPEN_PROJECT.getSelectionModel().selectedItemProperty().removeListener(selectedTabListener);
TAB_PANE_OPEN_PROJECT.getSelectionModel().clearSelection();
TAB_PANE_OPEN_PROJECT.getTabs().clear();
BUTTON_CONFIRM.disableProperty().unbind();
selectedTabListener = null;
languageListener = null;
tabOpenNewProject = null;
tabOpenExistingProject = null;
stage = null;
getChildren().clear();
}
private void addLanguageListener() {
this.languageBundle.addListener(languageListener);
}
private void languageChange(ObservableValue<? extends ResourceBundle> observable, ResourceBundle oldValue, ResourceBundle newValue) {
reloadElements();
}
private String getValueFromKey(String key) {
return this.languageBundle.getValue().getString(key);
}
private void reloadElements() {
// Nothing to do
}
public void setStage(Stage stage) {
this.stage = stage;
}
private void selectedTabChanged(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {
if (newValue.equals(tabOpenNewProject)) {
BUTTON_CONFIRM.setText(getValueFromKey(Keys.CREATE));
} else if (newValue.equals(tabOpenExistingProject)) {
BUTTON_CONFIRM.setText(getValueFromKey(Keys.OPEN));
}
}
}
For loading FXML files I use Singleton Class ScreenManager. This method is called to load this Dialog :
public void showNewDialog( FilesFXML fxml) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml.toString()), context.getBundleValue());
try {
Stage dialogStage = new Stage();
AnchorPane dialogwindow = (AnchorPane) loader.load();
FXMLDialogController controller = loader.getController();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(this.getStage());
Scene scene = new Scene(dialogwindow);
dialogStage.setScene(scene);
dialogStage.setResizable(false);
controller.setStage(dialogStage);
dialogStage.showAndWait();
} catch (Exception ex) {
log.error(ex.getMessage());
log.error(ex.getCause());
ex.printStackTrace();
}
}
I was checked it in VisualVM and I see one 1 of this controller and 2 lambdas ( I suppose it consiste initialization of 2 listener)
But event clear function is called this dialog is still in memory and cannot be garbage collected. I have no more idea how to force removing it. It is reallegrave, because it consist all of my Controllers.
I'm pretty confused what you are asking and what you are talking about. Nevertheless, if that dialog window is open, there is no way it can be garbage collected.
The JavaFX main API is holding a strong reference of the stage (maybe the scene too).
The stage/scene is holding strong reference of all the nodes specified in your FXML file.
Some of those nodes are holding strong reference of the controller because you have specified action event handlers in the controller.
In order to make the controller eligible for garbage collection, you need to break this chain somewhere.
The easiest way is to close the dialog window. When the window is closed, because the JavaFX engine does not need to manage render pulses anymore, it will release its reference to the stage. At that point, the whole thing under that stage is eligible for garbage collection, if nothing else (that itself is not eligible for garbage collection) is holding strong references of them.

How to access JavaFx node elements values in methods of FXML Controller class?

It seems values of nodes (textfields, checkboxes, etc...) can only be accessed via an event listener on them. Why am I saying that ?
- First I'm new to JavaFx
- Second : below is the scenario I'm using
public class FXMLDocumentController implements Initializable {
#FXML
private JFXCheckBox myCheckBox;
#FXML
private JFXButton myButton;
#Override
public void initialize(URL url, ResourceBundle rb) {
myCheckBox.addEventHandler(MouseEvent.MOUSE_CLICKED, (e)->{
if(myCheckBox.isSelected()){
System.out.Println("Printed from event handler");
});
myButton.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
myMethod();
});
}
public void myMethod(){
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
for(Node node : root.getChildrenUnmodifiable()){
if(node.getId().equals("myCheckBox"){
JFXCheckBox myChk = (JFXCheckBox) node;
if(myChk.isSelected()){
System.out.Println("Printed from my inner method");
}
System.out.Println(myChk.isSelected());
}
}
}
}
What happend is when I check the box, the event handler does how supposed. It prints Printed from event handler. But right afetr that, when clicking myButton, the myMethod always prints false.
When I use a class variable indicating if the checkbox is selected and read it in myMethod, it says the check box has been checked ...
What is going on ?
The same thing happens with textfields ...

JavaFX Change label text from another class with controller

I want to change the text of a Label with the controller from another class. I have made a method in the FXMLDocumentController, which sets the text to the label:
public void setLabelText(String text)
{
lbZeit.setText(text);
}
Now I want to change this text from another class like my SerialHandlerClass. First, I need the controller, am I right? So I did this:
FXMLLoader loader = new FXMLLoader(FXMLDocumentController.class.getResource("FXMLDocument.fxml"));
loader.load();
controller = (FXMLDocumentController) loader.getController();
Now I run the "setLabelText" method....
controller.setLabelText("asd");
... and nothing happens...
It's very funny, because when I add System.out.println(text); to the "setLabelText(String text)" method, the program writes the correct text to the console.
But, why?
Sorry for my bad english, it's not my native language :)
Thanks,
Julian
You are not updating the label because you are creating another instance of FXMLDocumentController when you use the FXMLoader.
You should set the controller instance, that contains the label, as a parameter to the other Class.
Below you have the code that could solve your need. Here I set the Controller instance to the Connector class, so you can call the setLabelText method from the other class:
public class Connector {
public static void Connecting(FXMLDocumentController controller) {
try {
System.out.println("Connector.Connecting(): Called");
controller.setLabelText("Bye World");
} catch (IOException ex) {
Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("FXMLDocumentController.#handleButtonAction");
label.setText("Hello World!");
Connector.Connecting(this);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setLabelText(String text)
{
System.out.println("FXMLDocumentController.setLabelText(): Called");
label.setText(text);
}
}
Note:
If your routine is going to take longer to execute whatever it needs to, you might want to use a Task, so you don't freeze your UI. To update the Label, you have to bind the text property and then update the Text value using the updateMessage() method.
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("FXMLDocumentController.#handleButtonAction");
label.setText("Hello World!");
Task<Boolean> connectorTask = new ConnectorTask();
label.textProperty().bind(connectorTask.messageProperty());
connectorTask.setOnSucceeded(e -> {
// this is going to be called if the task ends up without error
label.textProperty().unbind();
});
new Thread(connectorTask).start();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
//public void setLabelText(String text)
//{
// System.out.println("FXMLDocumentController.setLabelText(): Called");
// label.setText(text);
//}
public class ConnectorTask extends Task<Boolean> {
#Override
protected Boolean call() throws Exception {
// ... do whatever you need here
// then you call this method to update the TextProperty from the Label that was bound.
updateMessage("Bye World");
return Boolean.TRUE;
}
}
}
NOTE:
There is a possible duplicate question for this, please see my answer for this question here!

Resources