javafx doesnt display a nested component

▼魔方 西西 提交于 2020-02-08 09:58:52

问题


In continue to 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 componenet :

  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 isnt 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

回答1:


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 :)



来源:https://stackoverflow.com/questions/59785959/javafx-doesnt-display-a-nested-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!