Delali
Delali

Reputation: 908

Problems with creating custom control in JavaFx

I am trying to create a custom control in JavaFx. The main aim is to create a node that acts as a button. It is simply a Label that would be styled with css to look like a button. I have created a class MyControl that extends the control class like so:

public class MyControl extends Control {


@Override
protected String getUserAgentStylesheet() {
    return MyControl.class.getResource("myControl.css").toExternalForm();
}

public MyControl() {
    getStyleClass().add("new-control");
}
public MyControl(String item) {
    getStyleClass().add("new-control");
}
}
}

For the SkinBase class MyControlSkin I have:

public class MyControlSkin extends SkinBase<MyControlSkin> {
    public MyControlSkin(MyControlSkin control) {
        super(control);
        Label label = new Label("Some text here");

        getChildren.add(label);

    }
}

In the myControl.css file i simply have:

.tree-node-view {
   -fx-skin: "treeviewsample.TreeViewSkin";
}
.label {
    -fx-text-fill: -fx-text-background-color;
}

I have created the css for this but the problem is that I don't see the label displayed on the scene:

MyControl control = new MyControl();

but no Label displays on the screen. Please help me I am new to this so ask me for more information if in case my question does not make sense.

I am using javaFx 2.2

Upvotes: 2

Views: 949

Answers (1)

James_D
James_D

Reputation: 209225

I don't know if you made errors transferring the code from your IDE to the post, but as it stands the skin class will not compile. You need

public class MyControlSkin extends SkinBase<MyControl> {
    public MyControlSkin(MyControl control) {
        super(control);
        Label label = new Label("Some text here");

        getChildren().add(label);

    }
}

The css must define an fx:skin property for a class that matches the control. So you need

.new-control {
   -fx-skin: "MyControlSkin";
}
.label {
    -fx-text-fill: -fx-text-background-color;
}

Upvotes: 3

Related Questions