Francesco
Francesco

Reputation: 406

JavaFx on Eclipse, impossibility loading .fxml

I have created a new JavaFX project,and I can comfortably run.

Main:

package application; 

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            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);
    }
}

If I try to copy and paste some my old .fxml I receive some error.

The fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane prefHeight="691.0" prefWidth="668.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ControllerStart">
   <center>
      <Pane prefHeight="599.0" prefWidth="668.0" BorderPane.alignment="CENTER">
         <children>
            <Label layoutX="366.0" layoutY="38.0" text="10:20">
               <font>
                  <Font size="62.0" />
               </font>
            </Label>
            <Label layoutX="359.0" layoutY="111.0" text="00:00:00">
               <font>
                  <Font size="48.0" />
               </font>
            </Label>
            <Label layoutX="35.0" layoutY="66.0" text="Operator code:">
               <font>
                  <Font size="23.0" />
               </font>
            </Label>
            <Label layoutX="111.0" layoutY="101.0" text="000-000-00" />
            <CheckBox fx:id="CB_smcProblem" disable="true" layoutX="206.0" layoutY="211.0" mnemonicParsing="false" text="problem" />
            <CheckBox fx:id="CB_smcCheck" disable="true" layoutX="205.0" layoutY="242.0" mnemonicParsing="false" text="check" />
            <CheckBox fx:id="CB_smc" layoutX="72.0" layoutY="221.0" mnemonicParsing="false" onAction="#SMC_click" text="SMC" />
            <CheckBox fx:id="CB_sketch" layoutX="72.0" layoutY="330.0" mnemonicParsing="false" text="Sketch" />
            <CheckBox fx:id="CB_sketchProblem" disable="true" layoutX="206.0" layoutY="320.0" mnemonicParsing="false" text="problem" />
            <CheckBox fx:id="CB_sketchCheck" disable="true" layoutX="206.0" layoutY="351.0" mnemonicParsing="false" text="check" />
            <CheckBox fx:id="CB_configuration" layoutX="72.0" layoutY="457.0" mnemonicParsing="false" text="Configuration" />
            <CheckBox fx:id="CB_configurationProblem" disable="true" layoutX="207.0" layoutY="447.0" mnemonicParsing="false" text="problem" />
            <CheckBox fx:id="CB_configurationCheck" disable="true" layoutX="207.0" layoutY="478.0" mnemonicParsing="false" text="check" />
            <Button fx:id="B_closed" disable="true" layoutX="249.0" layoutY="591.0" mnemonicParsing="false" text="Closed">
               <font>
                  <Font size="35.0" />
               </font>
            </Button>
            <CheckBox fx:id="CB_supportH" layoutX="366.0" layoutY="221.0" mnemonicParsing="false" text="Support on helicopter" />
            <CheckBox fx:id="CB_qualityS" layoutX="364.0" layoutY="291.0" mnemonicParsing="false" text="Quality support " />
            <CheckBox fx:id="CB_releaseD" layoutX="365.0" layoutY="362.0" mnemonicParsing="false" text="Release of documents" />
            <CheckBox fx:id="CB_other" layoutX="368.0" layoutY="515.0" mnemonicParsing="false" text="Other" />
            <TextField fx:id="TF_other" editable="false" layoutX="483.0" layoutY="510.0" prefHeight="31.0" prefWidth="136.0" />
            <CheckBox fx:id="CB_requestC" layoutX="364.0" layoutY="426.0" mnemonicParsing="false" text="Request ciclie" />
            <Button layoutX="112.0" layoutY="613.0" mnemonicParsing="false" onAction="#SMC_click" text="Button" />
         </children>
      </Pane>
   </center>
</BorderPane>

This is the modification of main file:

        ...
        BorderPane root = new BorderPane(); //old part
        root= FXMLLoader.load(getClass().getResource("Start.fxml")); //new part
        Scene scene = new Scene(root,400,400); //old part
        ...

I have also added a class ControllerStart.java.

This is my error when i run the project now:

javafx.fxml.LoadException: /C:/eclipse/workspace/neon/TAP1/bin/application/Start.fxml:47

  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 application.Main.start(Main.java:15)
  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(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.TextField field application.ControllerStart.TF_other to javafx.scene.control.TextField
  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)
... 17 more

Upvotes: 0

Views: 633

Answers (1)

Michael
Michael

Reputation: 44090

The error line which is helpful for you is:

at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException:
Can not set java.awt.TextField field application.ControllerStart.TF_other to javafx.scene.control.TextField

Basically you have a field in your controller called TF_other which is an AWT TextField annotated with @FXML. You're probably mistakenly importing java.awt.TextField when you should be importing javafx.scene.control.TextField.


So funnily enough all of the code you posted is irrelevant and the code you didn't post is where the error is.

Upvotes: 2

Related Questions