JavaFx在FXML中包含自定义组件 [英] JavaFx Include custom components in FXML

查看:309
本文介绍了JavaFx在FXML中包含自定义组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试找到有关如何将自定义javafx对象包含到fxml文件中的解决方案.

例如

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

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

进入fxml

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

我总是从类型javafx.fxml.LoadException中获取错误代码: 也许有比创建自定义类更好的解决方案.但是我需要一个带有自定义接口的标签(已连接).也许另一种解决方案是创建一个仅包含标签的fxml文件,并使用接口为此设置控制器类.

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();
    }

更改名称后,我能够无错误地导入自定义对象 但是当我尝试插入

<MyLabel fx:id="myLabel"/>

在我的xml日期中,我收到此错误

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

应用程序启动方法异常

解决方案

Java有各种命名约定,包括类名应大写,程序包名称应全部小写,而字段和方法名应以小写开头.

尽管Java编译器和运行时仅将它们视为约定,并且将从上下文中确定任何实体是类还是属性(本质上不考虑名称),但对于FXML和FXMLLoader而言,情况并非如此. /p>

FXML文档状态:

如果元素的标签以大写字母开头,则该标签被视为实例声明

稍后

其标签名称以小写字母开头的元素表示对象属性.

因此,如果您的类未遵循通常的命名约定,则它们可能无法在FXML中正常工作.确保类和接口名称均使用大写字母,属性名称不使用大写字母,并且程序包名称均使用小写字母,并确保在Java代码和FXML中一致使用它们.

此外,FXMLLoader将通过(通常)调用无参数构造函数来创建类的实例.为了使其正常工作,构造函数必须是公共的:

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

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

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

解决方案

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 ...
}

这篇关于JavaFx在FXML中包含自定义组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆