java - 什么时候可以访问 FXML 属性?

标签 java javafx scenebuilder

几天来,我一直在尝试为 JavaFX 创建自定义组件,但遇到了一个问题。我的目的是创建一个带有自定义字段的组件,该组件的用户稍后可以对其进行编辑。

到目前为止一切顺利,该字段已经存在并且可以编辑。字段值是我的自定义字段,它可以被编辑。

奖励问题:1) 如何删除用户代理样式表字段? 2)该字段只接受正整数。我如何让它接受负整数?

The field "Value" is my custom field

问题是当我尝试从我的代码中访问这个值时。这是组件的 .jar 和 .fxml 的代码

简单.jar

public class Simple extends AnchorPane implements Initializable{

    public Simple(){
        //Loads the FXML sheet
        FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Simple.fxml") );
        fxmlLoader.setRoot(this); 
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        System.out.println( "Constructor: " + valueProperty.getValue() );
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println( "Init: " + valueProperty.getValue() );
    }


    @FXML protected void printValue(ActionEvent ae){
        System.out.println( "Button: " + valueProperty.getValue() );
    }

    //Editor field for the  value (helps scene builder to render it)
    IntegerProperty valueProperty;
    public void setValue(Integer value){
        valueProperty().setValue(value);
    }
    public Integer getValue(){
        return valueProperty == null ? -10 :  valueProperty.get();
    }
    public final IntegerProperty valueProperty() {
        if( valueProperty == null )
            valueProperty = new SimpleIntegerProperty(-10);
        return valueProperty;
    }
}

简单.fxml

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

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

<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">

      <Button mnemonicParsing="false" onAction="#printValue" text="Button" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />

</fx:root>

这些组件已导出为名为 gikkWidgets 的外部库,然后导入到另一个项目中。正如我上面所说,值字段可以在 Scene Builder 中访问,但我无法启动我的应用程序:

Exception in Application start method
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:497)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/D:/Project/gikkWidgets/bin/main/Test.fxml:12

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at main.Test.start(Test.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/198061478.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1595566805.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
    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$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    ... 1 more
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: 
/D:/Project/gikkWidgets/bin/com/gikk/javafx/simple/Simple.fxml

    at com.gikk.javafx.simple.Simple.<init>(Simple.java:26)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1005)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:742)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
    ... 22 more
Caused by: javafx.fxml.LoadException: 
/D:/Goats_Project/gikkWidgets/bin/com/gikk/javafx/simple/Simple.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
    at com.gikk.javafx.simple.Simple.<init>(Simple.java:24)
    ... 32 more
Caused by: java.lang.NullPointerException
    at com.gikk.javafx.simple.Simple.initialize(Simple.java:34)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 35 more
Exception running application main.Test

我猜它源于某种与创建属性的时间有关的问题。如果您注释掉 Simple.jar 中的代码,第 29 行和第 34 行(构造函数 sysout 和初始化 sysout),那么它会起作用并且按钮显示正确的值。但是,在我正在处理的实际组件中,我需要在构建时访问用户期望的值。这有可能吗?


编辑

Puce 的回答解决了最初的问题,我试图访问一个未初始化的变量。

但是,应用他建议的修复(即,将 valueProperty.getValue() 更改为简单的 getValue() 会产生以下输出:

Constructor: -10
Init: -10
Button: 5

即,属性值直到初始化后才设置。是否有可能以某种方式更早地读取属性值,以便在构造函数中使用它(或至少在 initialize 方法中)?

最佳答案

如果您对其进行注释,则可以将属性值作为参数提供给构造函数@NamedArg . (API 文档未提供任何信息:另请参见 here。)

因此(包括对您的属性实现的轻微更改):

public class Simple extends AnchorPane implements Initializable{

    private static final int DEFAULT_VALUE = -10 ;

    public Simple(@NamedArg("value") int value){
        //Loads the FXML sheet
        FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "Simple.fxml") );
        fxmlLoader.setRoot(this); 
        fxmlLoader.setController(this);

        // set value, avoiding property creation if default...
        if (value != DEFAULT_VALUE) {
            setValue(value);
        }

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        System.out.println( "Constructor: " + getValue() );
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println( "Init: " + getValue() );
    }


    @FXML protected void printValue(ActionEvent ae){
        System.out.println( "Button: " + getValue() );
    }

    //Editor field for the  value (helps scene builder to render it)
    IntegerProperty valueProperty;
    public final void setValue(Integer value){
        valueProperty().setValue(value);
    }
    public final Integer getValue(){
        return valueProperty == null ? DEFAULT_VALUE : valueProperty.get();
    }
    public final IntegerProperty valueProperty() {
        if( valueProperty == null )
            valueProperty = new SimpleIntegerProperty(DEFAULT_VALUE);
        return valueProperty;
    }
}

这是 JavaFX 8 及更高版本的功能:我不确定 SceneBuilder 是否可以完全使用它。

关于java - 什么时候可以访问 FXML 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32311558/

相关文章:

java - 删除一行数据后无法刷新TableView

java - 避免不在 FX 应用程序线程上导致 UI 崩溃

Java : Dirty Check for ArrayList?

java - 如何匹配这两个值相同?

java - 将javafx中的文本字段加密

java - 使用 SceneBuilder 在 JavaFX 中运行时创建新的 ImageView

java - 如何启用预定项目的下拉列表以添加到 SceneBuilder 中的自定义控件?

java - 需要帮助显示来自 url 或文件选择器的图像

java - "java.lang.NoSuchFieldError: NONE"与 Spring 3、maven、JPA、c3p0 hibernate

java - Spring过滤器作为基于Java注释的bean