FXML和/或Scene Builder是否可以识别默认方法? [英] Are default methods recognized by FXML and/or Scene Builder?

查看:86
本文介绍了FXML和/或Scene Builder是否可以识别默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的经验,标题中问题的答案是响亮的否!",我不敢相信这是真的……但就我的经验而言.我创建了具有3种方法的各种属性类型接口,例如:getColor(),setColor(Paint c)和ObjectProperty colorProperty(). get()/set()方法在接口中使用default修饰符在中实现.除涉及Scene Builder和FXML的情况外,这通常可以正常工作.使用Scene Builder时,上述属性不会显示,除非完全不显示接口中的实现,而在实现该接口的类中将其覆盖,这违背了使用默认方法的整个目的,...对吗?在使用上述属性的FXML中,对于仅在接口中实现的属性(没有上述覆盖)抛出PropertyNotFoundException.

Based on my experience the answer to the question in the Title is a resounding 'NO!!!', which I can't believe to be true ... but for my experience. I've created various property type interfaces having 3 methods, for example: getColor(), setColor(Paint c), and ObjectProperty colorProperty(). The get()/set() methods are implemented in the interface with the default modifier. This generally works fine except in cases involving Scene Builder and FXML. With Scene Builder the above property doesn't show at all unless the implementations in the interface are overridden in the class that implements the interface, which defeats the whole purpose of using default methods, ... right? With FXML using the above property, a PropertyNotFoundException is thrown for properties implemented only in the interface (without the aforementioned override).

示例代码:

public interface TestInterface {
    ObjectProperty<Paint> colorProperty();
    default Paint getColor() {return colorProperty().get();}
    default void setColor(Paint c) {colorProperty().set(c);}
}

public class TestClass implements TestInterface {
    private ObjectProperty<Paint> color;
    public ObjectProperty<Paint> colorProperty() {
        if (color == null)
            color = new SimpleObjectProperty<>(this, "color", Color.GREEN);
        return color;
    }
}

这是尝试运行包含FXML文件的测试应用程序而生成的错误跟踪代码段,该文件试图设置名为'pressedColor'的属性,该属性的声明和实现与上述示例代码中的'color'相同: /p>

Here's an error trace snippet generated from trying to run a test app that includes an FXML file that tries to set a property named 'pressedColor' which is declared and implemented in the same manner as 'color' in sample code above:

javafx.fxml.LoadException:
...
Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "pressedColor" does not exist or is read-only.

以下是FXML文件中令人讨厌的代码段:

Here's the offending snippet from the FXML file:

<RectangleButton pressedColor="#ff6e6e">

对于任何指导将不胜感激.谢谢!

Would be grateful for any guidance. Thanks!

推荐答案

我已经创建了功能请求.将来可能会对此问题提供有效的答案: https: //bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8259916

I have created a feature request. Maybe there will be an valid answer to this question in the future: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8259916

这篇关于FXML和/或Scene Builder是否可以识别默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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