JavaFx Include custom components in FXML

你。 提交于 2019-12-11 16:54:37

问题


i try to find a solution on how to include custom javafx objects to a fxml file.

For example

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

    MyLabel(){
        super();
    }
  //Custom Code ...
}

into fxml

<?import myExtendedObjects.myLabel?>
<myLabel text="Name" />

i alsways get error codes from the type javafx.fxml.LoadException: Maybe there is a better solution then creating custom classes. But i need a Label with a custom Interface (connected). maybe a other solution would be to creata a fxml file wich includes only a label and set up a controller class for this with the interface.

EDIT:

try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Viewer.fxml"));
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        controller = fxmlLoader.getController();
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }

EDITS 2: After changing Names, i was able to import the custom object without error but when i try to insert

<MyLabel fx:id="myLabel"/>

in my xml date i get this error

javafx.fxml.LoadException: 
/C:/Users/TheOLGPC/Desktop/java/SolarimpactTelemety2/bin/fxml/Viewer.fxml:48

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1013)
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 application.Main.start(Main.java:24)
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.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a member of class myExtendedObjects.MyLabel with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1009)
... 15 more

Exception in Application start method


回答1:


Java has various naming conventions, including that class names should be capitalized, package names should be all lower case, and field and method names should begin lower case.

While the Java compiler and runtime regards these only as conventions, and will determine whether any entity is a class or property from the context, essentially regardless of name, the same is not true in FXML and for the FXMLLoader.

The FXML documentation states:

an element's tag is considered an instance declaration if the tag begins with uppercase letter

and later

Elements whose tag names begin with a lowercase letter represent object properties.

So if your classes fail to follow the usual naming conventions, they may fail to work correctly in FXML. Make sure the class and interface names are capitalized, property names are not capitalized, and package names are all lower case, and make sure they are used consistently in the Java code and in the FXML.

Additionally, the FXMLLoader will create an instance of your class by invoking (usually) the no-argument constructor. In order for that to work, the constructor must be public:

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

    public MyLabel(){
        super();
    }
  //Custom Code ...
}


来源:https://stackoverflow.com/questions/44438555/javafx-include-custom-components-in-fxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!