Javafx can not set element field to AnchorPane










0















I have a problem with a javaFX project.



I have a fxml including another one :



<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" stylesheets="@../css/mainWindow.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.MainWindowController">
<children>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<ListView fx:id="gameMenu" onMouseClicked="#handleGameMenuClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1600.0" style="-fx-background-color: orange;" BorderPane.alignment="CENTER" />
</top>
<left>
<ListView fx:id="planetMenu" onMouseClicked="#handlePlanetMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: blue;" />
</left>
<right>
<ListView fx:id="empireMenu" onMouseClicked="#handleEmpireMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</right>
<center>
<fx:include fx:id="mainElement" source="MainElementView.fxml"/>
</center>
</BorderPane>
</children>
</AnchorPane>


In the controller, I use



@FXML
private MainElementViewController mainElement;
To access to the included element



Here is the MainElementView.fxml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController">
<children>

</children>
</AnchorPane>


The ode of MainElementViewController.java :



package com.spaceopera.client.ihm.mainwindow.mainelement;

import com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class MainElementViewController extends AnchorPane implements Initializable

@FXML
SpecificElementViewController specificElement;

private List<String> elementTypesList;

public MainElementViewController()



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


public List<String> getElementTypesList()
return elementTypesList;


public void setElementTypesList(List<String> elementTypesList)
System.out.println("plop");
this.elementTypesList = elementTypesList;
specificElement.setElementTypes(elementTypesList);





And here is the SpecificElementView.xml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.ListView?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController">
<children>
<ListView fx:id="elementTypes" onMouseClicked="#handleSpecificElementClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1200.0" style="-fx-background-color: purple;" />
</children>
</AnchorPane>


And the java :



package com.spaceopera.client.ihm.mainwindow.mainelement.specificelement;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class SpecificElementViewController extends AnchorPane implements Initializable

private List<String> elementTypesList;

@FXML
private ListView elementTypes;

public SpecificElementViewController()


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


@FXML
public void handleSpecificElementClick(MouseEvent arg) throws Exception
throw new Exception("Not implemented yet!!!");


public List<String> getElementTypes()
return elementTypesList;


public void setElementTypes(List<String> elementTypesList)
this.elementTypes = elementTypes;
elementTypes.getItems().clear();
for (String s : elementTypesList)
System.out.println("type : " + s);
elementTypes.getItems().add(s);





When i run it, I have an exception :




avafx.fxml.LoadException:
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at
com.spaceopera.client.Client.startGame(Client.java:88) at
com.spaceopera.client.Client.start(Client.java:62) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at
com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)2018-11-11 20:10:11 ERROR
LoggerTool:32 - Error starting main window :
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



Caused by: java.lang.IllegalArgumentException: Can not set
com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController
field
com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController.specificElement
to javafx.scene.layout.AnchorPane at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764) at
javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163) at
javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 19 more




If I remove the include in MainElementView.fxml, I have the same exception on the include in the MainWindow.fxml (with problem to set MainElement to anchorpane instead of specificElement).



Can you help me to fix this pls?










share|improve this question






















  • You need to append Controller to the fx:id to get the name of the field where the controller of the <fx:include> element is is injected. If you use the fx:id itself as field name, the object created for the root elment of the included fxml is injected.

    – fabian
    Nov 11 '18 at 20:14











  • I lost 4 hours because of that... Thanks for your help I didn't know the "controller" was important in the name.

    – oliver39
    Nov 11 '18 at 22:02
















0















I have a problem with a javaFX project.



I have a fxml including another one :



<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" stylesheets="@../css/mainWindow.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.MainWindowController">
<children>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<ListView fx:id="gameMenu" onMouseClicked="#handleGameMenuClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1600.0" style="-fx-background-color: orange;" BorderPane.alignment="CENTER" />
</top>
<left>
<ListView fx:id="planetMenu" onMouseClicked="#handlePlanetMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: blue;" />
</left>
<right>
<ListView fx:id="empireMenu" onMouseClicked="#handleEmpireMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</right>
<center>
<fx:include fx:id="mainElement" source="MainElementView.fxml"/>
</center>
</BorderPane>
</children>
</AnchorPane>


In the controller, I use



@FXML
private MainElementViewController mainElement;
To access to the included element



Here is the MainElementView.fxml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController">
<children>

</children>
</AnchorPane>


The ode of MainElementViewController.java :



package com.spaceopera.client.ihm.mainwindow.mainelement;

import com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class MainElementViewController extends AnchorPane implements Initializable

@FXML
SpecificElementViewController specificElement;

private List<String> elementTypesList;

public MainElementViewController()



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


public List<String> getElementTypesList()
return elementTypesList;


public void setElementTypesList(List<String> elementTypesList)
System.out.println("plop");
this.elementTypesList = elementTypesList;
specificElement.setElementTypes(elementTypesList);





And here is the SpecificElementView.xml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.ListView?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController">
<children>
<ListView fx:id="elementTypes" onMouseClicked="#handleSpecificElementClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1200.0" style="-fx-background-color: purple;" />
</children>
</AnchorPane>


And the java :



package com.spaceopera.client.ihm.mainwindow.mainelement.specificelement;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class SpecificElementViewController extends AnchorPane implements Initializable

private List<String> elementTypesList;

@FXML
private ListView elementTypes;

public SpecificElementViewController()


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


@FXML
public void handleSpecificElementClick(MouseEvent arg) throws Exception
throw new Exception("Not implemented yet!!!");


public List<String> getElementTypes()
return elementTypesList;


public void setElementTypes(List<String> elementTypesList)
this.elementTypes = elementTypes;
elementTypes.getItems().clear();
for (String s : elementTypesList)
System.out.println("type : " + s);
elementTypes.getItems().add(s);





When i run it, I have an exception :




avafx.fxml.LoadException:
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at
com.spaceopera.client.Client.startGame(Client.java:88) at
com.spaceopera.client.Client.start(Client.java:62) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at
com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)2018-11-11 20:10:11 ERROR
LoggerTool:32 - Error starting main window :
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



Caused by: java.lang.IllegalArgumentException: Can not set
com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController
field
com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController.specificElement
to javafx.scene.layout.AnchorPane at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764) at
javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163) at
javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 19 more




If I remove the include in MainElementView.fxml, I have the same exception on the include in the MainWindow.fxml (with problem to set MainElement to anchorpane instead of specificElement).



Can you help me to fix this pls?










share|improve this question






















  • You need to append Controller to the fx:id to get the name of the field where the controller of the <fx:include> element is is injected. If you use the fx:id itself as field name, the object created for the root elment of the included fxml is injected.

    – fabian
    Nov 11 '18 at 20:14











  • I lost 4 hours because of that... Thanks for your help I didn't know the "controller" was important in the name.

    – oliver39
    Nov 11 '18 at 22:02














0












0








0








I have a problem with a javaFX project.



I have a fxml including another one :



<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" stylesheets="@../css/mainWindow.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.MainWindowController">
<children>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<ListView fx:id="gameMenu" onMouseClicked="#handleGameMenuClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1600.0" style="-fx-background-color: orange;" BorderPane.alignment="CENTER" />
</top>
<left>
<ListView fx:id="planetMenu" onMouseClicked="#handlePlanetMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: blue;" />
</left>
<right>
<ListView fx:id="empireMenu" onMouseClicked="#handleEmpireMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</right>
<center>
<fx:include fx:id="mainElement" source="MainElementView.fxml"/>
</center>
</BorderPane>
</children>
</AnchorPane>


In the controller, I use



@FXML
private MainElementViewController mainElement;
To access to the included element



Here is the MainElementView.fxml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController">
<children>

</children>
</AnchorPane>


The ode of MainElementViewController.java :



package com.spaceopera.client.ihm.mainwindow.mainelement;

import com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class MainElementViewController extends AnchorPane implements Initializable

@FXML
SpecificElementViewController specificElement;

private List<String> elementTypesList;

public MainElementViewController()



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


public List<String> getElementTypesList()
return elementTypesList;


public void setElementTypesList(List<String> elementTypesList)
System.out.println("plop");
this.elementTypesList = elementTypesList;
specificElement.setElementTypes(elementTypesList);





And here is the SpecificElementView.xml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.ListView?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController">
<children>
<ListView fx:id="elementTypes" onMouseClicked="#handleSpecificElementClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1200.0" style="-fx-background-color: purple;" />
</children>
</AnchorPane>


And the java :



package com.spaceopera.client.ihm.mainwindow.mainelement.specificelement;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class SpecificElementViewController extends AnchorPane implements Initializable

private List<String> elementTypesList;

@FXML
private ListView elementTypes;

public SpecificElementViewController()


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


@FXML
public void handleSpecificElementClick(MouseEvent arg) throws Exception
throw new Exception("Not implemented yet!!!");


public List<String> getElementTypes()
return elementTypesList;


public void setElementTypes(List<String> elementTypesList)
this.elementTypes = elementTypes;
elementTypes.getItems().clear();
for (String s : elementTypesList)
System.out.println("type : " + s);
elementTypes.getItems().add(s);





When i run it, I have an exception :




avafx.fxml.LoadException:
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at
com.spaceopera.client.Client.startGame(Client.java:88) at
com.spaceopera.client.Client.start(Client.java:62) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at
com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)2018-11-11 20:10:11 ERROR
LoggerTool:32 - Error starting main window :
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



Caused by: java.lang.IllegalArgumentException: Can not set
com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController
field
com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController.specificElement
to javafx.scene.layout.AnchorPane at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764) at
javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163) at
javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 19 more




If I remove the include in MainElementView.fxml, I have the same exception on the include in the MainWindow.fxml (with problem to set MainElement to anchorpane instead of specificElement).



Can you help me to fix this pls?










share|improve this question














I have a problem with a javaFX project.



I have a fxml including another one :



<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" stylesheets="@../css/mainWindow.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.MainWindowController">
<children>
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1200.0" prefWidth="1600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<ListView fx:id="gameMenu" onMouseClicked="#handleGameMenuClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1600.0" style="-fx-background-color: orange;" BorderPane.alignment="CENTER" />
</top>
<left>
<ListView fx:id="planetMenu" onMouseClicked="#handlePlanetMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: blue;" />
</left>
<right>
<ListView fx:id="empireMenu" onMouseClicked="#handleEmpireMenuClick" minHeight="1100.0" minWidth="200.0" maxHeight="1100.0" maxWidth="200.0" prefHeight="1100.0" prefWidth="200.0" style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</right>
<center>
<fx:include fx:id="mainElement" source="MainElementView.fxml"/>
</center>
</BorderPane>
</children>
</AnchorPane>


In the controller, I use



@FXML
private MainElementViewController mainElement;
To access to the included element



Here is the MainElementView.fxml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController">
<children>

</children>
</AnchorPane>


The ode of MainElementViewController.java :



package com.spaceopera.client.ihm.mainwindow.mainelement;

import com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class MainElementViewController extends AnchorPane implements Initializable

@FXML
SpecificElementViewController specificElement;

private List<String> elementTypesList;

public MainElementViewController()



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


public List<String> getElementTypesList()
return elementTypesList;


public void setElementTypesList(List<String> elementTypesList)
System.out.println("plop");
this.elementTypesList = elementTypesList;
specificElement.setElementTypes(elementTypesList);





And here is the SpecificElementView.xml :



<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.ListView?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="1200.0" stylesheets="@../css/specificElement.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController">
<children>
<ListView fx:id="elementTypes" onMouseClicked="#handleSpecificElementClick" orientation="HORIZONTAL" prefHeight="100.0" prefWidth="1200.0" style="-fx-background-color: purple;" />
</children>
</AnchorPane>


And the java :



package com.spaceopera.client.ihm.mainwindow.mainelement.specificelement;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
*
* @author olivier
*/
public class SpecificElementViewController extends AnchorPane implements Initializable

private List<String> elementTypesList;

@FXML
private ListView elementTypes;

public SpecificElementViewController()


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


@FXML
public void handleSpecificElementClick(MouseEvent arg) throws Exception
throw new Exception("Not implemented yet!!!");


public List<String> getElementTypes()
return elementTypesList;


public void setElementTypes(List<String> elementTypesList)
this.elementTypes = elementTypes;
elementTypes.getItems().clear();
for (String s : elementTypesList)
System.out.println("type : " + s);
elementTypes.getItems().add(s);





When i run it, I have an exception :




avafx.fxml.LoadException:
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at
com.spaceopera.client.Client.startGame(Client.java:88) at
com.spaceopera.client.Client.start(Client.java:62) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at
com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)2018-11-11 20:10:11 ERROR
LoggerTool:32 - Error starting main window :
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27



Caused by: java.lang.IllegalArgumentException: Can not set
com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController
field
com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController.specificElement
to javafx.scene.layout.AnchorPane at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764) at
javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163) at
javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 19 more




If I remove the include in MainElementView.fxml, I have the same exception on the include in the MainWindow.fxml (with problem to set MainElement to anchorpane instead of specificElement).



Can you help me to fix this pls?







java javafx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '18 at 19:12









oliver39oliver39

14




14












  • You need to append Controller to the fx:id to get the name of the field where the controller of the <fx:include> element is is injected. If you use the fx:id itself as field name, the object created for the root elment of the included fxml is injected.

    – fabian
    Nov 11 '18 at 20:14











  • I lost 4 hours because of that... Thanks for your help I didn't know the "controller" was important in the name.

    – oliver39
    Nov 11 '18 at 22:02


















  • You need to append Controller to the fx:id to get the name of the field where the controller of the <fx:include> element is is injected. If you use the fx:id itself as field name, the object created for the root elment of the included fxml is injected.

    – fabian
    Nov 11 '18 at 20:14











  • I lost 4 hours because of that... Thanks for your help I didn't know the "controller" was important in the name.

    – oliver39
    Nov 11 '18 at 22:02

















You need to append Controller to the fx:id to get the name of the field where the controller of the <fx:include> element is is injected. If you use the fx:id itself as field name, the object created for the root elment of the included fxml is injected.

– fabian
Nov 11 '18 at 20:14





You need to append Controller to the fx:id to get the name of the field where the controller of the <fx:include> element is is injected. If you use the fx:id itself as field name, the object created for the root elment of the included fxml is injected.

– fabian
Nov 11 '18 at 20:14













I lost 4 hours because of that... Thanks for your help I didn't know the "controller" was important in the name.

– oliver39
Nov 11 '18 at 22:02






I lost 4 hours because of that... Thanks for your help I didn't know the "controller" was important in the name.

– oliver39
Nov 11 '18 at 22:02













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252234%2fjavafx-can-not-set-element-field-to-anchorpane%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252234%2fjavafx-can-not-set-element-field-to-anchorpane%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)