Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Setting Controller for <fx:include> element

993317Mar 7 2013 — edited Mar 11 2013
Is there a way to set the controller for the <fx:include> element before loading the fxml of the same ?? This is what i am trying but it seems like the FXMLLoader which is loading the <fx:include> element is setting some other controller for the same. Is there a way to access the FXMLLoader loading the <fx:include> and set my own controller before loading the fxml ???

package fxminclude;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

public class ParentController implements Initializable {

    @FXML
    private TextField nameField;
    @FXML
    private TextField valueField;
    @FXML
    private HBox title;
    @FXML
    private TitleController titleController;
    private Model model;

    public ParentController(Model model) {
        this.model = model;
        titleController = new TitleController(model);
    }

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

        nameField.setText(model.nameProperty().get());
        model.nameProperty().bind(nameField.textProperty());

        valueField.setText(model.valueProperty().get());
        model.valueProperty().bind(valueField.textProperty());

        titleController.initialize(location, resources);

    }
}
package fxminclude;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

public class TitleController implements Initializable {

    @FXML
    private TextField titleField;
    private Model model;

    public TitleController(Model model) {
        this.model = model;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        titleField.setText(model.titleProperty().get());           // Getting NPE here
        model.titleProperty().bind(titleField.textProperty());
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox prefHeight="412.0" prefWidth="352.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <HBox alignment="CENTER" prefHeight="53.0" prefWidth="352.0">
      <children>
        <Label id="Name" text="Name" />
        <TextField fx:id="nameField" prefWidth="200.0" />
      </children>
    </HBox>
    <HBox alignment="CENTER" prefHeight="47.0" prefWidth="352.0">
      <children>
        <Label text="Value" />
        <TextField fx:id="valueField" prefWidth="200.0" />
      </children>
    </HBox>
    <Pane prefHeight="312.0" prefWidth="352.0">
      <children>
        <fx:include fx:id="title" source="title.fxml" layoutX="42.0" />   //here
      </children>
    </Pane>
  </children>
</VBox>
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<HBox alignment="CENTER" prefHeight="38.0" prefWidth="269.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <Label text="Title" />
    <TextField fx:id="titleField" prefWidth="200.0" />
  </children>
</HBox>
package fxminclude;

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;

public class ParentView {

    protected VBox template;

    public ParentView(ParentController controller) {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
        loader.setController(controller);
        try {
            loader.load();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        template = loader.getRoot();
    }

    public VBox getTemplate() {
        return template;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2013
Added on Mar 7 2013
2 comments
4,648 views