In this snippet of code, a view is created with a custom control:
public BasicView(String name) {
super(name);
setCenter(new StackPane(new ChoiceTextField()));
}
class ChoiceTextField<T> extends Control {
public ChoiceTextField() {
getStyleClass().add("button");
}
@Override
protected Skin<?> createDefaultSkin() {
return new ChoiceFieldSkin<>(this);
}
}
class ChoiceFieldSkin<T> extends SkinBase<ChoiceTextField<T>> {
private final TextField textfield;
public ChoiceFieldSkin(ChoiceTextField<T> control) {
super(control);
textfield = new TextField();
getChildren().add(textfield);
}
}
However, an exception is encountered:
javafx.scene.control.Control loadSkinClass
Failed to load skin 'com.gluonhq.impl.charm.a.b.a.ap' for control ChoiceTextField@4fb753dd[styleClass=button]
java.lang.IllegalArgumentException: argument type mismatch
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:423)
at javafx.scene.control.Control.loadSkinClass(Control.java:735)
The javadoc for Control.createDefaultSkin()
explains that a default skin is created if no skin is provided via CSS {@code -fx-skin} or set explicitly in a sub-class with {@code setSkin(...)}.
By adding the style class "button", Charm overrides the ButtonSkin and causes the exception to occur.
To avoid this exception, the skin can be explicitly set via CSS:
public ChoiceTextField() {
getStyleClass().addAll("choice-field", "button");
getStylesheets().add(getClass().getResource("style.css").toExternalForm());
}
.choice-field {
-fx-skin: '<package name>.ChoiceFieldSkin'
}