问题
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