У меня проблема с проектом javaFX.
У меня есть fxml, включая еще один:
<?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>
В контроллере я использую
@ FXML
частный MainElementViewController mainElement;
Для доступа к включенному элементу
Вот 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>
Ода 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);
}
}
А вот 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>
И 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);
}
}
}
Когда я запускаю его, у меня возникает исключение:
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
в
javafx.fxml.FXMLLoader.constructLoadException (FXMLLoader.java:2601)
в javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2579) в
javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2441) в
javafx.fxml.FXMLLoader.access $ 2700 (FXMLLoader.java:103) в
javafx.fxml.FXMLLoader $ IncludeElement.constructValue (FXMLLoader.java:1143)
в
javafx.fxml.FXMLLoader $ ValueElement.processStartElement (FXMLLoader.java:746)
в javafx.fxml.FXMLLoader.processStartElement (FXMLLoader.java:2707)
в javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2527) в
javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2441) в
javafx.fxml.FXMLLoader.load (FXMLLoader.java:2409) в
com.spaceopera.client.Client.startGame (Client.java:88) в
com.spaceopera.client.Client.start (Client.java:62) в
com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 161 (LauncherImpl.java:863)
в
com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 174 (PlatformImpl.java:326)
в
com.sun.javafx.application.PlatformImpl.lambda $ обнулить $ 172 (PlatformImpl.java:295)
в java.security.AccessController.doPrivileged (собственный метод) в
com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 173 (PlatformImpl.java:294)
в
com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (InvokeLaterDispatcher.java:95)
в com.sun.glass.ui.gtk.GtkApplication._runLoop (собственный метод) в
com.sun.glass.ui.gtk.GtkApplication.lambda $ обнулить $ 48 (GtkApplication.java:139)
at java.lang.Thread.run (Thread.java:748) 2018-11-11 20:10:11 ОШИБКА
LoggerTool: 32 - Ошибка запуска главного окна:
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainElementView.fxml:15
/home/olivier/spaceOpera/Client_V2/target/classes/fxml/MainWindow.fxml:27
Вызвано: java.lang.IllegalArgumentException: невозможно установить
com.spaceopera.client.ihm.mainwindow.mainelement.specificelement.SpecificElementViewController
поле
com.spaceopera.client.ihm.mainwindow.mainelement.MainElementViewController.specificElement
в javafx.scene.layout.AnchorPane в
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException (UnsafeFieldAccessorImpl.java:167)
в
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException (UnsafeFieldAccessorImpl.java:171)
в
sun.reflect.UnsafeObjectFieldAccessorImpl.set (UnsafeObjectFieldAccessorImpl.java:81)
в java.lang.reflect.Field.set (Field.java:764) в
javafx.fxml.FXMLLoader.injectFields (FXMLLoader.java:1163) в
javafx.fxml.FXMLLoader.access $ 1600 (FXMLLoader.java:103) в
javafx.fxml.FXMLLoader $ ValueElement.processValue (FXMLLoader.java:857)
в
javafx.fxml.FXMLLoader $ ValueElement.processStartElement (FXMLLoader.java:751)
в javafx.fxml.FXMLLoader.processStartElement (FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2527) ... еще 19
Если я удаляю включение в MainElementView.fxml, у меня возникает то же исключение для включения в MainWindow.fxml (с проблемой установки MainElement в опорную область вместо specificElement).
Можете ли вы помочь мне исправить это пожалуйста?