0

I know how to parse variables to controllers in JavaFX with fxml. But i need to use them in the initialize method of my controller. Is there a ways to do this? The background is, that i have a interface, where you can define different settings. Now you can safe them and have to be able to reopen them. So now when i open a rule, i need to set the values in the new option view. I know, that it works on text fields (UI-Elements) to set Text during initialize but not for variables. I tried different approaches. Like binding with properties (works for visibility property of button (UI-Element) but not for variables to set. Do you know a way or maybe an other approach?

Here is my example:

Controller1:

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

/**
 *
 * @author Sandro
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Button btn_openWindow;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        try {
            Stage stage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Parent root = loader.load(getClass().getResource("fxml_second.fxml").openStream());
            Fxml_secondController cont = (Fxml_secondController)loader.getController();
            cont.setFlag(0x00000002);
            cont.setIsChange(false);
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        btn_openWindow.setOnAction(this::handleButtonAction);
    }    

Controller 2:

    /**
 * FXML Controller class
 *
 * @author Sandro
 */
public class Fxml_secondController implements Initializable {

    @FXML private Button btn_printOut;

    private boolean isChange = true;
    private int flag = 0x00000001;

    private void printOut(ActionEvent event){
        System.out.println("isChange: "+isChange);
        System.out.println("flag: "+flag);
    }

    public boolean isIsChange() {
        return isChange;
    }

    public void setIsChange(boolean isChange) {
        this.isChange = isChange;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        btn_printOut.setOnAction(this::printOut);
        System.out.println(flag);
    }    

In controller 2 you see the problem. The console-output in initialize shows 1 but it need to show 2. If i klick on printOut (Button) it prints out the right values which i have set in Controller 1.

Steinliiippp
  • 373
  • 4
  • 20

1 Answers1

1

Set the controller in the Java code, instead of in FXML.

Remove the fx:controller attribute from fxml_second.fxml, and change the code in FXMLDocumentController as follows:

@FXML
private void handleButtonAction(ActionEvent event) {
    try {
        Stage stage = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml_second.fxml"));
        Fxml_secondController cont = new Fxml_secondController();
        cont.setFlag(0x00000002);
        cont.setIsChange(false);
        loader.setController(cont);
        Parent root = loader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    } catch (IOException ex) {
        Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Another option would be to use a custom component approach for the second fxml.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Netbeans say: no suitable constructor found for FXMLLoader(InputStream) constructor FXMLLoader.FXMLLoader(URL) is not applicable (argument mismatch; InputStream cannot be converted to URL) constructor FXMLLoader.FXMLLoader(Charset) is not applicable (argument mismatch; InputStream cannot be converted to Charset) – Steinliiippp Oct 12 '15 at 22:17
  • Ok found the solution. The .openStream() method produce the error. It works. Thank you! – Steinliiippp Oct 12 '15 at 22:19
  • Yes, sorry: I copied and pasted and didn't notice the `openStream()` call there. In general, you should prefer a URL rather than a stream when you have use a `FXMLLoader`. I fixed the answer. – James_D Oct 13 '15 at 00:37
  • Is it all so possible to set TextField values (ui-values) with this approach? I tried, but i get an IO-Excption. Are this fields not initializied? How can i set this values? – Steinliiippp Oct 27 '15 at 15:21
  • @Steinliiippp That sounds like a completely different question. – James_D Oct 27 '15 at 15:24
  • Ohh ok ... i will try it by my self. Thougt it is the same tactic. – Steinliiippp Oct 27 '15 at 15:34
  • Well maybe I don't understand what you are asking. Either way, try it, and ask a question if you can't do it. – James_D Oct 27 '15 at 15:50
  • Hello, i ask a new question http://stackoverflow.com/questions/33387009/javafx-how-to-set-values-during-in-initialize .It would be nice if you can take a look at it. THANK YOU – Steinliiippp Oct 28 '15 at 09:15