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?
java javafx
add a comment |
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?
java javafx
You need to appendController
to thefx:id
to get the name of the field where the controller of the<fx:include>
element is is injected. If you use thefx: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
add a comment |
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?
java javafx
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
java javafx
asked Nov 11 at 19:12
oliver39
14
14
You need to appendController
to thefx:id
to get the name of the field where the controller of the<fx:include>
element is is injected. If you use thefx: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
add a comment |
You need to appendController
to thefx:id
to get the name of the field where the controller of the<fx:include>
element is is injected. If you use thefx: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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You need to append
Controller
to thefx:id
to get the name of the field where the controller of the<fx:include>
element is is injected. If you use thefx: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