简体   繁体   中英

How do I use more than one fxml with javaFX?

Currently, my main javaFX main class looks something like this:

public class Main extends Application {

private Stage primaryStage;
private AnchorPane rootLayout;
private Shell shl = new Shell();;
@FXML
private PasswordField pass;
@FXML
private TextField user;
@FXML
private String description;
@FXML
private String title;


@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("WorkFlow Manager");
    initRootLayout();
}

/**
 * root layout (base layout).
 */
public void initRootLayout() {
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("LoginMenu.fxml"));
        rootLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);                                    
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void toDoLayout() {
    try{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("Todo.fxml"));
        AnchorPane toDo = (AnchorPane) loader.load();
        Scene scene = new Scene(rootLayout);

    }catch(IOException e) {
        e.printStackTrace();
    }
}

/**
 *
 * @return primaryStage
 */
public Stage getPrimaryStage() {
    return primaryStage;
}

public static void main(String[] args) {
    launch(args);
}

@FXML
private void login(ActionEvent event) {
    if(user.getText()!=null && pass.getText()!=null){
        shl.login(user.getText(),pass.getText());
        System.out.printf("%s",shl.getCurrentUser());
        //toDoLayout(); I commented this out since it isn't working
    }
}
}

My first function, initRootLayout(), is working fine, loads the fxml as it's supposed to and executes the expected functions on action.

However, what I'm trying is to launch a new scene (replacing the login fxml scene). I tried to call the toDoLayout() function (it's commented out) as it would throw these exceptions everytime I clicked the button:

 java.lang.ClassNotFoundException: main.iface.MainTerminal{
/C:/Users/a51217/workspace/wfmgr/bin/main/iface/Todo.fxml:9
  at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at main.iface.Main.toDoLayout(Main.java:59)
  at main.iface.Main.login(Main.java:84)
  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.reflect.misc.Trampoline.invoke(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at javafx.scene.control.Button.fire(Unknown Source)
  at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
  at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.notifyMouse(Unknown Source)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
  at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)

javafx.fxml.LoadException: java.lang.ClassNotFoundException: main.iface.MainTerminal{
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at main.iface.Main.toDoLayout(Main.java:59)
    at main.iface.Main.login(Main.java:84)
    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.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: main.iface.MainTerminal{
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 63 more

EDIT: My Todo.fxml:

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

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


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.iface.MainTerminal{">
   <children>
      <ListView layoutX="37.0" layoutY="47.0" prefHeight="333.0" prefWidth="195.0" />
      <ScrollBar layoutX="37.0" layoutY="47.0" orientation="VERTICAL" prefHeight="333.0" prefWidth="14.0" />
      <TextField fx:id="getTitle" layoutX="262.0" layoutY="47.0" prefHeight="25.0" prefWidth="321.0" />
      <TextArea fx:id="getDescription" layoutX="262.0" layoutY="92.0" prefHeight="247.0" prefWidth="321.0" />
      <Button layoutX="262.0" layoutY="355.0" mnemonicParsing="false" text="Create Task" />
      <Button layoutX="351.0" layoutY="355.0" mnemonicParsing="false" text="Assign User to this Task" />
      <ScrollBar layoutX="262.0" layoutY="92.0" orientation="VERTICAL" prefHeight="247.0" prefWidth="14.0" />
   </children>
</AnchorPane>

MainTerminal isn't my controller, I forgot to correct it. My Main is acting as the controller so I changed the 9th line to:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.iface.Main{">

After this change, the exceptions are the seem to be the same:

java.lang.ClassNotFoundException: main.iface.Main{
/C:/Users/a51217/workspace/wfmgr/bin/main/iface/Todo.fxml:9
  at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
  at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at main.iface.Main.toDoLayout(Main.java:59)
  at main.iface.Main.login(Main.java:84)
  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.reflect.misc.Trampoline.invoke(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at javafx.scene.control.Button.fire(Unknown Source)
  at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
  at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.notifyMouse(Unknown Source)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
  at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)

javafx.fxml.LoadException: java.lang.ClassNotFoundException: main.iface.Main{
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
    at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at main.iface.Main.toDoLayout(Main.java:59)
    at main.iface.Main.login(Main.java:84)
    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.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: main.iface.Main{
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 63 more

The exception is occurring because you have an extra { in the fx:controller attribute. This attribute should be set to the fully-qualified name of the controller class. Since { is not a legal character in a class name, fx:controller = "main.iface.MainTerminal{" cannot possibly be correct.

Assuming your controller class is MainTerminal and is in a package called main.iface , the correct attribute setting is fx:controller = "main.iface.MainTerminal" .

Additionally, your toDoLayout() method just creates a Scene containing the UI defined in ToDo.fxml, but doesn't do anything with that scene: you need to display it in a stage. So once you have fixed the fx:controller attribute, it will run without errors but not do anything until you also fix that code.

Finally, it looks like you are using your Application subclass as a controller for (at least?) one of your FXML files, as you have @FXML -annotated fields in it. Do not do this: it will be highly confusing. (What happens is that one instance of your Main class is created at startup for you, and then another instance is created by the FXMLLoader for whichever FXML file declares this as the controller. Some fields will be initialized in the first instance - primaryStage and rootLayout - and other fields (the @FXML -annotated ones) will be initialized in the second instance. It will be very very difficult to keep track of what is initialized and what is still null .) You should create the Application subclass just to start the application up, and then each FXML should have its own, separate, controller class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM