Passing data from one controller to another in javafx. java.lang.IllegalArgumentException - javafx

I am creating a reporting application in which I have 3 scenes with 3 controller classes. My aim is to click on a button (Send Email) from my MaintainanceBacklog_Controller Class, which uses 3 ObservableList from the same controller class and 1 ObservableList from another controller class(BatchProcesses_Controller). I need to pass this one List from BatchProcesses_Controller to my MaintainanceBacklog_Controller. I am getting
Caused by: java.lang.IllegalArgumentException: Can not set reporting.controllers.BatchProcesses_Controller field reporting.controllers.MainScreen_Controller.batchProcesses_Controller to javafx.scene.layout.AnchorPane
I am attaching my code snippets for these three Controller classes + main Class + all three fxml files snippets + Error Stack trace..
Please help. Not able to figure out the issue.
MainScreen_Controller.java
package reporting.controllers;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import reporting.model.BatchProcess_OnlySuccessCount;
import reporting.controllers.BatchProcesses_Controller;
import reporting.controllers.MaintainanceBacklog_Controller;
public class MainScreen_Controller {
#FXML private BatchProcesses_Controller batchProcesses_Controller;
#FXML private MaintainanceBacklog_Controller maintainanceBacklog_Controller;
public void initialize() {
batchProcesses_Controller.init(this);
maintainanceBacklog_Controller.init(this);
}
public ObservableList<BatchProcess_OnlySuccessCount>
getBatchProcessListData() {
return batchProcesses_Controller.data_SuccessCount;
}
}
BatchProcesses_Controller.java
.
.//other #FXML declarations
.
#FXML
private DatePicker datePck_To;
private MainScreen_Controller mainScreen;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert cmbBox_Product != null : "fx:id=\"cmbBox_Product\" was not
injected: check your FXML file 'MaintainanceBacklog_BatchProcesses.fxml'.";
dateCol.setCellValueFactory(new PropertyValueFactory<>("Date"));
...//other column setCellValues here
showGraphCol.setCellValueFactory(new PropertyValueFactory<>
("showGraphBtn"));
}
//injecting main screen controller to this class
public void init(MainScreen_Controller mainScreen_Controller) {
mainScreen = mainScreen_Controller;
}
MaintainanceBacklog_Controller.java
...//other imports
import reporting.util.Maint_ProcessesEnum;
import reporting.controllers.MainScreen_Controller;
public class MaintainanceBacklog_Controller implements Initializable {
...//other FXM`enter code here`L declarations
private MainScreen_Controller main;
#Override
public void initialize(URL location, ResourceBundle resources) {
//other table column setCellValuesFactorys
}
public void onButtonClick_EmailReport() {
EmailGenerator email = new EmailGenerator();
email.setDataInTable(data_after, data_before,data_businessErr,
main.getBatchProcessListData());
}//calling getBatchProcessListData to get the required list
Main.java
public class Main extends Application {private Parent rootNode;
public static void main(String[] args) {
launch(args);
}
#Override
public void init() throws Exception {
FXMLLoader fxmlLoader = new
FXMLLoader(reporting.design.Test.class.getResource("MainScreen.fxml"));
rootNode = fxmlLoader.load();
}
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("RSA Reporting");
stage.setScene(new Scene(rootNode));
stage.show();
}
MainScreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="833.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="reporting.controllers.MainScreen_Controller">
<children>
<TabPane prefHeight="791.0" prefWidth="1046.0">
<tabs>
<Tab text="BatchProcess">
<content>
<fx:include fx:id="batchProcesses_Controller" source="BatchProcesses.fxml" />
</content>
</Tab>
<Tab text="MaintainanceBacklog">
<content>
<fx:include fx:id="maintainanceBacklog_Controller" source="MaintainanceBacklog.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
BatchProcesses.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="batchProcesses" prefHeight="791.0" prefWidth="1046.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="reporting.controllers.BatchProcesses_Controller">
<children>
<Label layoutX="194.0" layoutY="73.0" prefHeight="17.0" prefWidth="82.0" text="Select Process" />
<ComboBox fx:id="cmbBox_Product" layoutX="288.0" layoutY="69.0" prefHeight="25.0" prefWidth="174.0" promptText="Select">
<items>
<FXCollections fx:factory="observableArrayList">
...
MaintainanceBacklog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="maintainanceBacklog" prefHeight="791.0" prefWidth="1046.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="reporting.controllers.MaintainanceBacklog_Controller">
<children>
<ComboBox fx:id="cmbBox_Product_MaintainanceBacklog" layoutX="151.0" layoutY="52.0" prefHeight="25.0" prefWidth="313.0" promptText="Select">
<items>
<FXCollections fx:factory="observableArrayList">
...
ErrorStackTrace
log4j:WARN No appenders could be found for logger (reporting.util.Util).
log4j:WARN Please initialize the log4j system properly.
Apr 20, 2018 3:37:16 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.141 by JavaFX runtime of version 8.0.122-ea
Apr 20, 2018 3:37:16 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.141 by JavaFX runtime of version 8.0.122-ea
Exception in Application init method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/SyntBotsServices/ProductionReporting/bin/reporting/design/MainScreen.fxml:13
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.load(FXMLLoader.java:2409)
at application.Main.init(Main.java:72)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Caused by: java.lang.IllegalArgumentException: Can not set reporting.controllers.BatchProcesses_Controller field reporting.controllers.MainScreen_Controller.batchProcesses_Controller to javafx.scene.layout.AnchorPane
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
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)
... 6 more
Exception running application application.Main

Since the root element of BatchProcesses.fxml is a <AnchorPane> element, an AnchorPane is created for the following line.
<fx:include fx:id="batchProcesses_Controller" source="BatchProcesses.fxml" />
The field in your controller corresponding to the value of the fx:id attribute is
#FXML private BatchProcesses_Controller batchProcesses_Controller;
FXMLLoader fails to assign a AnchorPane to a field of type BatchProcesses_Controller for obvious reasons.
If you need the controller of the included fxml to be assigned to a controller field, you need to use append Controller to the fx:id of the <fx:include>:
#FXML private AnchorPane batchProcesses_Controller;
#FXML private BatchProcesses_Controller batchProcesses_ControllerController;

Related

JavaFX - java.lang.reflect.InvocationTargetException when executing WebView exemple code

I tried to run an example of WebView project and I had this error
C:\Users\Dell\.jdks\openjdk-19.0.1\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2022.2.4\lib\idea_rt.jar=50766:D:\IntelliJ IDEA 2022.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\Dell\Desktop\javafx-dialogs-0.0.4.jar;C:\Users\Dell\.m2\repository\org\icepdf\os\icepdf-viewer\6.1.2\icepdf-viewer-6.1.2.jar;C:\Users\Dell\.m2\repository\org\icepdf\os\icepdf-core\6.1.2\icepdf-core-6.1.2.jar;C:\Users\Dell\.m2\repository\org\bouncycastle\bcprov-jdk15on\1.54\bcprov-jdk15on-1.54.jar;C:\Users\Dell\.m2\repository\org\bouncycastle\bcprov-ext-jdk15on\1.54\bcprov-ext-jdk15on-1.54.jar;C:\Users\Dell\.m2\repository\org\bouncycastle\bcpkix-jdk15on\1.54\bcpkix-jdk15on-1.54.jar;C:\Users\Dell\.m2\repository\javax\media\jai-core\1.1.3\jai-core-1.1.3.jar;C:\Users\Dell\.m2\repository\com\sun\media\jai_imageio\1.1\jai_imageio-1.1.pom;C:\Users\Dell\.m2\repository\batik\batik-awt-util\1.6\batik-awt-util-1.6.jar;C:\Users\Dell\.m2\repository\batik\batik-dom\1.6\batik-dom-1.6.jar;C:\Users\Dell\.m2\repository\batik\batik-svg-dom\1.6\batik-svg-dom-1.6.jar;C:\Users\Dell\.m2\repository\batik\batik-svggen\1.6\batik-svggen-1.6.jar;C:\Users\Dell\.m2\repository\batik\batik-util\1.6\batik-util-1.6.jar;C:\Users\Dell\.m2\repository\batik\batik-xml\1.6\batik-xml-1.6.jar;C:\Users\Dell\.m2\repository\org\jfxcore\javafx-media\18-ea+1\javafx-media-18-ea+1.jar;C:\Users\Dell\.m2\repository\org\jfxcore\javafx-graphics\18-ea+1\javafx-graphics-18-ea+1.jar;C:\Users\Dell\.m2\repository\org\jfxcore\javafx-base\18-ea+1\javafx-base-18-ea+1.jar;C:\Users\Dell\.m2\repository\org\eclipse\jetty\websocket\websocket-api\9.4.46.v20220331\websocket-api-9.4.46.v20220331.jar;C:\Users\Dell\.m2\repository\org\webjars\webjars-locator\0.30\webjars-locator-0.30.jar;C:\Users\Dell\.m2\repository\org\webjars\webjars-locator-core\0.30\webjars-locator-core-0.30.jar;C:\Users\Dell\.m2\repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;C:\Users\Dell\.m2\repository\org\apache\commons\commons-compress\1.9\commons-compress-1.9.jar;C:\Users\Dell\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.3.3\jackson-databind-2.3.3.jar;C:\Users\Dell\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.3.0\jackson-annotations-2.3.0.jar;C:\Users\Dell\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.3.3\jackson-core-2.3.3.jar;C:\Users\Dell\.m2\repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-controls\19-ea+7\javafx-controls-19-ea+7.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-graphics\19-ea+7\javafx-graphics-19-ea+7.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-base\19-ea+7\javafx-base-19-ea+7.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-fxml\19-ea+7\javafx-fxml-19-ea+7.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-web\19-ea+7\javafx-web-19-ea+7.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-media\19-ea+7\javafx-media-19-ea+7.jar;C:\Users\Dell\.m2\repository\net\synedra\validatorfx\0.3.1\validatorfx-0.3.1.jar -p "C:\Users\Dell\IdeaProjects\PFEL\target\classes;C:\Users\Dell\.m2\repository\org\openjfx\javafx-web\19-ea+7\javafx-web-19-ea+7-win.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-fxml\19-ea+7\javafx-fxml-19-ea+7-win.jar;C:\Users\Dell\.m2\repository\org\kordamp\bootstrapfx\bootstrapfx-core\0.4.0\bootstrapfx-core-0.4.0.jar;D:\MI\3LMD\memoire\Nouveau dossier\JavaFxLibs\mysql-connector-java-5.1.42-bin.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-controls\19-ea+7\javafx-controls-19-ea+7-win.jar;C:\Users\Dell\.m2\repository\org\kordamp\ikonli\ikonli-javafx\12.3.1\ikonli-javafx-12.3.1.jar;C:\Users\Dell\.m2\repository\org\kordamp\ikonli\ikonli-core\12.3.1\ikonli-core-12.3.1.jar;C:\Users\Dell\.m2\repository\org\jfxcore\javafx-graphics\18-ea+1\javafx-graphics-18-ea+1-win.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-graphics\19-ea+7\javafx-graphics-19-ea+7-win.jar;C:\Users\Dell\.m2\repository\org\jfxcore\javafx-media\18-ea+1\javafx-media-18-ea+1-win.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-media\19-ea+7\javafx-media-19-ea+7-win.jar;C:\Users\Dell\.m2\repository\com\dlsc\formsfx\formsfx-core\11.5.0\formsfx-core-11.5.0.jar;C:\Users\Dell\.m2\repository\org\jfxcore\javafx-base\18-ea+1\javafx-base-18-ea+1-win.jar;C:\Users\Dell\.m2\repository\org\openjfx\javafx-base\19-ea+7\javafx-base-19-ea+7-win.jar;C:\Users\Dell\.m2\repository\org\controlsfx\controlsfx\11.1.1\controlsfx-11.1.1.jar" -m com.example.pfel/com.example.pfel.Elearning
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at javafx.graphics#18-ea/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics#18-ea/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics#18-ea/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics#18-ea/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: javafx.fxml.LoadException:
/C:/Users/Dell/IdeaProjects/PFEL/target/classes/com/example/pfel/Html.fxml:11
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
at com.example.pfel/com.example.pfel.Elearning.start(Elearning.java:14)
at javafx.graphics#18-ea/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics#18-ea/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:497)
at javafx.graphics#18-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:470)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#18-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:469)
at javafx.graphics#18-ea/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics#18-ea/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#18-ea/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:180)
... 1 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml#19-ea/javafx.fxml.JavaFXBuilderFactory$ObjectBuilderWrapper$ObjectBuilder.build(JavaFXBuilderFactory.java:237)
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:774)
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
at javafx.fxml#19-ea/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
... 12 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at javafx.fxml#19-ea/javafx.fxml.JavaFXBuilderFactory$ObjectBuilderWrapper$ObjectBuilder.build(JavaFXBuilderFactory.java:229)
... 15 more
Caused by: java.lang.NoSuchMethodError: 'void com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet()'
at javafx.controls#19-ea/javafx.scene.control.Control.<clinit>(Control.java:99)
at javafx.web#19-ea/com.sun.javafx.webkit.theme.ScrollBarThemeImpl$1.invalidated(ScrollBarThemeImpl.java:84)
at javafx.base#18-ea/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:348)
at javafx.base#18-ea/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base#18-ea/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base#18-ea/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base#18-ea/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.base#18-ea/javafx.beans.property.ObjectProperty.setValue(ObjectProperty.java:78)
at javafx.web#19-ea/javafx.scene.web.WebEngine.setView(WebEngine.java:997)
at javafx.web#19-ea/javafx.scene.web.WebView.<init>(WebView.java:276)
at javafx.web#19-ea/com.sun.javafx.fxml.builder.web.WebViewBuilder.build(WebViewBuilder.java:66)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 17 more
Exception running application com.example.pfel.Elearning
Process finished with exit code 1
and this is the fxml file and the controller
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.pfel.Html">
<children>
<WebView fx:id="webview" layoutX="6.0" layoutY="4.0" prefHeight="333.0" prefWidth="590.0" />
<Button layoutX="27.0" layoutY="361.0" mnemonicParsing="false" onAction="#loadpage" text="load" />
<TextField fx:id="textfield" layoutX="145.0" layoutY="361.0" onAction="#loadpage" />
</children>
</AnchorPane>
Controller:
package com.example.pfel;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import java.net.URL;
import java.util.ResourceBundle;
public class Html implements Initializable {
#FXML
private WebView webview;
#FXML
private TextField textfield;
private WebEngine engine;
#Override
public void initialize(URL location, ResourceBundle resources) {
engine = webview.getEngine();
loadpage();
}
public void loadpage(){
}
}

Why I can not use FXML with Controllers and fxid in controller?

I found here what I need. I need access from 1 Controller to another.
But I have a question. Why when I use FXML annotaion I get exception?
I want to make
public class MainController {
#FXML Tab1Controller tab1Controller;
#FXML Tab2Controller tab2Controller;
#FXML Tab tab1; //THIS
#FXML Tab tab2; // And this call exception. But if i comment both fields is evertying OK.
#FXML public void initialize() {
System.out.println("Application started");
tab1Controller.init(this);
tab2Controller.init(this);
}
public String loadLblTextFromTab1() {
return tab1Controller.lbl1.getText();
}
public void setTab2LabelText(String text) {
tab2Controller.lbl2.setText(text);
}
}
P.S add the XML file where I have that fx:id
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="299.0" prefWidth="309.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
<children>
<TabPane prefHeight="299.0" prefWidth="309.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab closable="false" text="Tab 1">
<content>
<fx:include fx:id="tab1" source="tab/Tab1.fxml" />
</content></Tab>
<Tab closable="false" text="Tab 2">
<content>
<fx:include fx:id="tab2" source="tab/Tab2.fxml" />
</content></Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
PSS.
If I use my FXML and Class like this I will get NullPointerException because of tab1Controller == null
public class MainController {
#FXML Tab tab1;
#FXML Tab1Controller tab1Controller;
#FXML public void initialize() {
tab1Controller.init(this); // Line 15
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.MainController">
<TabPane>
<Tab fx:id="tab1" closable="false" text="Tab 1">
<fx:include source="tab/Tab1.fxml"/>
</Tab>
</TabPane>
</AnchorPane>
Exception looks like
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 17 more
Caused by: java.lang.NullPointerException
at controller.MainController.initialize(MainController.java:15)
... 27 more
So, now I can not understand why I get that exception? Anyway I generated my Tab from fxml file itself.
The error message:
Caused by: java.lang.IllegalArgumentException:
Can not set javafx.scene.control.Tab field controller.MainController.tab1 to javafx.scene.layout.AnchorPane
explains what the problem is. The <fx:include> generates an AnchorPane (presumably that is the root element of Tab1.fxml), and you are trying to use a field of type Tab to reference it.
If you only need access to the content of the tab, just change the type of the field accordingly:
public class MainController {
#FXML Tab1Controller tab1Controller;
#FXML Tab2Controller tab2Controller;
#FXML AnchorPane tab1;
#FXML AnchorPane tab2;
#FXML public void initialize() {
System.out.println("Application started");
tab1Controller.init(this);
tab2Controller.init(this);
}
public String loadLblTextFromTab1() {
return tab1Controller.lbl1.getText();
}
public void setTab2LabelText(String text) {
tab2Controller.lbl2.setText(text);
}
}
If you specifically need access to the tabs themselves, then you'll need fx:ids on the Tab. As before, to get a reference to the controller, you need an fx:id on the <fx:include>. So if you want an actual reference to the Tab as well as to the controller, you will need (different) fx:ids on both the Tab and the <fx:include>:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="299.0" prefWidth="309.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
<children>
<TabPane prefHeight="299.0" prefWidth="309.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab fx:id="tab1" closable="false" text="Tab 1">
<content>
<fx:include fx:id="tab1Content" source="tab/Tab1.fxml" />
</content></Tab>
<Tab fx:id="tab2" closable="false" text="Tab 2">
<content>
<fx:include fx:id="tab2Content" source="tab/Tab2.fxml" />
</content></Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
and
public class MainController {
#FXML Tab1Controller tab1ContentController;
#FXML Tab2Controller tab2ContentController;
#FXML Tab tab1;
#FXML Tab tab2;
#FXML public void initialize() {
System.out.println("Application started");
tab1Controller.init(this);
tab2Controller.init(this);
}
public String loadLblTextFromTab1() {
return tab1ContentController.lbl1.getText();
}
public void setTab2LabelText(String text) {
tab2ContentController.lbl2.setText(text);
}
}

Cant launch application using JAVAFX FXML file

I am trying to launch the IntLogin window from the main class (MAAWB); however, I can not seem to get it to launch. My splash screen launches without issues. I ran a debug with no issues until it hits the launch sequence then it kicks back an unrecognizable error. I have included below the main, fxml and controller file. What am I missing or doing wrong here that's causing me all these headaches? Also if anyone has the answer how do I put my splash screen on a timer?
main
package javafxswingapplication3;
import javafx.application.Application;
import javafx.application.Preloader;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MAAWB extends Application {
#Override
public void start(Stage primaryStage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("Intlogin.fxml"));
Stage stage = new Stage();
stage.setTitle("initial login");
stage.setScene(new Scene(root, 222, 484));
stage.show();
} catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Preloader.launch(MAAWBPreloader.class, args);
Application.launch(MAAWB.class, args);
}
}
fxml
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.effect.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="222.0" prefWidth="484.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxswingapplication3.IntloginController">
<children>
<TextField layoutX="173.0" layoutY="50.0" />
<TextField fx:id="username" editable="false" layoutX="173.0" layoutY="90.0" />
<ChoiceBox layoutX="173.0" layoutY="130.0" prefHeight="31.0" prefWidth="187.0" />
<Label layoutX="91.0" layoutY="55.0" text="Installation" />
<Label layoutX="94.0" layoutY="95.0" text="Username" />
<Label layoutX="58.0" layoutY="135.0" text="Functional Area" />
<Button cancelButton="true" layoutX="400.0" layoutY="182.0" mnemonicParsing="false" text="Cancel" />
<Button layoutX="327.0" layoutY="182.0" mnemonicParsing="false" text="Submit" />
</children>
</AnchorPane>
controller
package javafxswingapplication3;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Properties;
import static javafx.application.ConditionalFeature.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
public class IntloginController implements Initializable {
private TextField username;
#Override
public void initialize(URL url, ResourceBundle rb) {
final String UN = System.getProperty("user.name");
username.setText(UN);
}
}
error
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.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at javafxswingapplication3.MAAWB.start(MAAWB.java:31)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at javafxswingapplication3.IntloginController.initialize(IntloginController.java:28)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
Annotate the username definition #FXML.
#FXML
private TextField username;
This will associate the TextField defined in your controller with the textfield defined in your FXML via fx:id="username".

Copy entite fxml data to different container

I am having two vbox(es).
First vbox fx:id is vbox1
Second vbox fx:id is vbox2
In vbox1 I am having textbox, combobox, buttons and everything else.
I am having one button which want to copy(onclick) entire source/fxml from vbox1 to vbox2.
Is there anyway to do that?
Define the content of the VBoxes in a separate FXML file. You can include the content in the first VBox directly in your "main" fxml with a <fx:include>:
<VBox fx:id="vbox1">
<fx:include source="Content.fxml"/>
</VBox>
and then you can load another copy in the button's handler with
#FXML
public void handleButtonAction(ActionEvent e) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Content.fxml"));
vbox2.getChildren().add(loader.load());
}
Complete example (everything in a package called application):
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<center>
<HBox spacing="5">
<VBox fx:id="vbox1">
<fx:include source="Content.fxml"/>
</VBox>
<VBox fx:id="vbox2"/>
</HBox>
</center>
<bottom>
<Button text="Load" onAction="#load" BorderPane.alignment="CENTER"/>
</bottom>
</BorderPane>
MainController.java:
package application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
public class MainController {
#FXML
private VBox vbox1 ;
#FXML
private VBox vbox2 ;
#FXML
private void load() throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Content.fxml"));
vbox2.getChildren().add(loader.load());
}
}
Content.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>
<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://javafx.com/fxml/1">
<TextField promptText="Text Field"/>
<ComboBox>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="One"/>
<String fx:value="Two"/>
<String fx:value="Three"/>
</FXCollections>
</items>
</ComboBox>
<Button text="Click me"/>
</VBox>
Main.java:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX. Communication between controllers

Hello I'm starting in JavaFX and I've got some problems.
I want to communicate two controlles from two diferents views. How can I do it?
I'm working with tabs and i have this two controllers and i want to do something similar like this:
Application.java:
public class JavaFXApplication6 extends Application
{
#Override
public void start(Stage primaryStage)
{
try
{
Parent root = FXMLLoader.load(getClass().getResource(
"/view/FXML_Main.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e){
e.printStackTrace();
}
primaryStage.setTitle("Back-end GUI");
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
FXML_Main.fxml:
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="controller.FXML_MainController">
<children>
<TabPane layoutX="61.0" layoutY="30.0" prefHeight="400.0" prefWidth="600.0"
tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<fx:include source="FXML_Tab1.fxml" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<fx:include source="FXML_Tab2.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
FXML_MainController.java:
public class FXML_MainController implements Initializable {
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXML_Tab1.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.FXML_Tab1Controller">
<children>
<Label fx:id="Label1" layoutX="282.0" layoutY="108.0" text="Label" />
<TextField fx:id="FextField1" layoutX="215.0" layoutY="146.0" />
<Button fx:id="Button1" layoutX="269.0" layoutY="197.0" mnemonicParsing="false"
onAction="#actionButton1" text="Button" />
</children>
</AnchorPane>
FXML_Tab1Controller.java:
public class FXML_Tab1Controller implements Initializable {
FXML_Tab2Controller tab2controller;
#FXML public Label Label1;
#FXML public TextField TextField1;
#FXML public Button Button1;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML private void actionButton1(ActionEvent event)
{
Label1.setText(tab2controller.TextField2.getText());
}
}
FXML_Tab2.fxml:
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.FXML_Tab2Controller">
<children>
<Label fx:id="Label2" layoutX="282.0" layoutY="99.0" text="Label" />
<TextField fx:id="TextField2" layoutX="215.0" layoutY="149.0" />
<Button fx:id="Button2" layoutX="270.0" layoutY="200.0" mnemonicParsing="false"
onAction="#actionButton2" text="Button" />
</children>
</AnchorPane>
FXML_Tab2Controller.java:
public class FXML_Tab2Controller implements Initializable {
FXML_Tab1Controller tab1controller;
#FXML public Label Label2;
#FXML public TextField TextField2;
#FXML public Button Button2;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML private void actionButton2(ActionEvent event){
Label2.setText(tab1controller.TextField1.getText());
}
}
something similar like that video:
https://www.youtube.com/watch?v=XLVx46ycxco
Add an fx:id to each of your <fx:include> tags:
<tabs>
<Tab text="Untitled Tab 1">
<content>
<fx:include source="FXML_Tab1.fxml" fx:id="tab1" />
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<fx:include source="FXML_Tab2.fxml" fx:id="tab2" />
</content>
</Tab>
</tabs>
This will allow you to inject the corresponding controllers into your FXML_MainController:
public class FXML_MainController {
#FXML
private FXML_Tab1Controller tab1Controller ;
#FXML
private FXML_Tab2Controller tab2Controller ;
}
The variable naming is very important here: the fields must be named xController where x is the value for the fx:id attribute in the fx:include. See the Introduction to FXML documentation for details.
Now in your main controller's initialize() method, you can establish the relationship between the two controllers:
public class FXML_MainController {
#FXML
private FXML_Tab1Controller tab1Controller ;
#FXML
private FXML_Tab2Controller tab2Controller ;
public void initialize() {
tab1Controller.tab2Controller = tab2Controller ;
tab2Controller.tab1Controller = tab1Controller ;
}
}

Resources