Are default methods recognized by FXML and/or Scene Builder?

给你一囗甜甜゛ 提交于 2021-01-29 04:03:42

问题


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

Sample code:

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

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.

Here's the offending snippet from the FXML file:

<RectangleButton pressedColor="#ff6e6e">

Would be grateful for any guidance. Thanks!


回答1:


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



来源:https://stackoverflow.com/questions/40360371/are-default-methods-recognized-by-fxml-and-or-scene-builder

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