-1

To continue this post, I implemented part of the solution that was suggested.

I have my main panel and I want to drew on it a component which is a different class (steerwheel).

My main controller :

public class WindowController {

    @FXML SteerWheel steerwheel;
     ... other componenets..
}

My new component :

  public SteeringWheel() {
  myLabel = new Label();
  innerCircle = new Circle();
  backgroundCircle = new Circle();
  System.out.println("steerwheel created.");

  }
  
  .. other methods..

My mainwindow.fxml file :

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import view.SteerWheel?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
   <center>
    <SteerWheel fx:id="steerwheel" />
   </center>
</BorderPane>

and my steerwheel.fxml file :

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="view.SteerWheel"
            prefHeight="400.0" prefWidth="600.0">

    <Label fx:id="mylabel" prefHeight="30.0" prefWidth="102.0" text="mytest"  translateY="30" translateX="90">
      <StackPane >
        <Circle fx:id="innerCircle" fill="darkgray" radius="170"  />
        <Circle fx:id="backgroundCircle " fill="black"  radius="80" />
    </StackPane>
    
</AnchorPane>

My main code that loads the fxml files (in a different file from all mentioned) :

public class Main extends Application {
public static Stage primaryStage;

@Override
public void start(Stage primary_stage) {
    this.primaryStage=primary_stage;
FXMLLoader fxl=new FXMLLoader();
try {
    BorderPane root = fxl.load(getClass().getResource("mainwindow.fxml").openStream());

    WindowController wc=fxl.getController(); 
    Scene scene = new Scene(root,700,700);
    primaryStage.setScene(scene);
    primaryStage.show();
} catch (IOException e) {
    e.printStackTrace();
}

I'm seeing the constructor`s output but to the console but the component isn't displayed on the window.

Update

I tried to add a call for the fxml file of the steerwheel in my Window.fxml :

<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
    <center>
        <VBox maxHeight="-Infinity" prefHeight="450.0" prefWidth="400.0" BorderPane.alignment="TOP_CENTER">
        <children>
            <fx:include source="steerwheel.fxml" fx:id="steerwheel" />

        </children>
        </VBox>

    </center>

</BorderPane>

Now I'm getting the following error :

Caused by: java.lang.IllegalArgumentException: Can not set view.steerWheel field view.WindowController.steerWheel to javafx.scene.layout.AnchorPane

halfer
  • 19,824
  • 17
  • 99
  • 186
JeyJ
  • 3,582
  • 4
  • 35
  • 83
  • 1
    [mcve] please .. – kleopatra Jan 17 '20 at 11:35
  • 1
    Assuming that constructor code is complete: you do not add anything to that layout. Furthermore the default property of `BorderPane` is `children` and using `BorderPane.getChildren()` is problematic. Add something to your layout in that constructor and wrap the `` element in a `
    ` element.
    – fabian Jan 17 '20 at 15:59

1 Answers1

0

I found the following post that described the same issue I had : Passing data from one controller to another in javafx. java.lang.IllegalArgumentException @fabian also answered there how to solve the issue.

The solution I implemented : My main fxml file :

<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
    <center>
        <VBox maxHeight="-Infinity" prefHeight="450.0" prefWidth="400.0" BorderPane.alignment="TOP_CENTER">
        <children>

        <fx:include fx:id="steerwheel" source="steerwheel.fxml"  />
        </children>
        </VBox>
    </center>

</BorderPane>

In the controler of the fxml file I added an AnchorPane object that named after the fx:id and I renamed the steerWheel obj to steerwheelController :

public class WindowController {
    @FXML AnchorPane steerwheel;
    @FXML steerWheel steerwheelController;

Afterwards, I had to use the setLocation method in my main in order to load the inner fxml file (otherwise I got an error..) :

FXMLLoader fxl=new FXMLLoader();
    try {
        fxl.setLocation(getClass().getResource("Window.fxml"));
        BorderPane root = fxl.load();
        WindowController wc=fxl.getController();

Hoping it will help someone :)

JeyJ
  • 3,582
  • 4
  • 35
  • 83