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

时间:2015-08-31 12:41:46

标签: java javafx scenebuilder

我已经玩弄了几天为JavaFX创建自定义组件而且我遇到了问题。我的目的是创建一个带有自定义字段的组件,以后可以由组件的用户进行编辑。

到目前为止一直很好,该领域在那里,可以编辑。字段值是我的自定义字段,可以进行编辑。

奖金问题:1)如何摆脱用户代理样式表字段? 2)该字段仅接受正整数。如何让它接受负整数?

The field "Value" is my custom field

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

的代码

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

Simple.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)中注释掉代码,那么它可以工作,按钮显示正确的值。但是,在我正在处理的实际组件中,我需要在构建时访问用户所需的值。这有可能吗?

修改

深褐色&#39;答案解决了最初的问题,我试图访问一个未初始化的变量。

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

Constructor: -10
Init: -10
Button: 5

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

3 个答案:

答案 0 :(得分:1)

而不是:

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

尝试:

 System.out.println( "Init: " + getValue() );

答案 1 :(得分:1)

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

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

$content = str_replace('<div id='demo'><a href='https://www.example.com' target='_blank'><img alt='Demo' src='//example.com/image.png' /></a></div>', '', $content);

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

答案 2 :(得分:0)

James_D显示了一种解决这个问题的方法,但它需要一个小小的推文,我在这里发布以供参考。

我必须更改两个构造函数才能使它工作。删除zero-args构造函数并更改另一个:

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

    if(value != null )
        setValue(value);
    else
        setValue(DEFAULT_VALUE);

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



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

此实施有效。如果.fxml文件中不存在字段“value”,则参数将为null,我们可以手动将值设置为DEFAULT_VALUE。