Javafx can not set element field to AnchorPane











up vote
0
down vote

favorite












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 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 at 22:02

















up vote
0
down vote

favorite












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 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 at 22:02















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 at 19:12









oliver39

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 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 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 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 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 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 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 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 at 22:02



















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',
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






























active

oldest

votes













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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?