JavaFX 8 issues with -fx-background css style selector

I am trying to create a custom button control. I have created a MyButton class starting at Control as shown below.

public class MyButton extends Control {
    private StringProperty textProperty=null;

    public MyButton() {
        this("Button");
    }

    public MyButton(String text) {
        setSkin(new MyButtonSkin(this));
        setText(text);
    }

    public final StringProperty textProperty() {
        if(textProperty==null) {
            textProperty=new SimpleStringProperty();
        }
        return textProperty;
    }

    public final String getText() {
        return textProperty().get();
    }

    public final void setText(String text) {
        textProperty().set(text);
    }
}

      

As you can see on line 9, I am using a skin called MyButtonSkin which I will show you shortly

public class MyButtonSkin extends SkinBase<MyButton>{
    private final Paint DEFAULT_BACKGROUND_COLOR=Color.GRAY;

    private Background background;

    private Label label;

    public MyButtonSkin(MyButton control) {
        super(control);
        label=new Label();
        label.textProperty().bind(control.textProperty());


        getChildren().add(label);

        initGraphics();
        initListeners();
    }

    private void initGraphics() {
        background=new Background(new BackgroundFill(
                DEFAULT_BACKGROUND_COLOR, new CornerRadii(20), new Insets(0, 0, 0, 0)));
        getSkinnable().setBackground(background);
    }

    private void initListeners() {
        getSkinnable().layoutBoundsProperty().addListener(obs-> {
            Bounds b=getSkinnable().layoutBoundsProperty().get();
            label.resizeRelocate(
                    b.getMinX(), b.getMinY(), b.getWidth(), b.getHeight());
        });

        //handler for mouse pressed and released are omitted because
        // I think they are not important in order to solve my problem

    }

    protected double computePrefWidth(double height, 
            double topInset, double rightInset, double bottomInset, double leftInset) {
        return label.prefWidth(height);
    }
}

      

This is what the button looks like

blank button

Now if I add style to the scene builder like

-fx-background-color: yellow

      

It seems that

button with -fx-background-color: yellow style

The problem I am facing is why the -fx-background-color selector style to change corner radii and insert background too? Same thing, if I only use -fx-background-radius, it also replaces the default color and padding. Shouldn't the -fx-background-color selector change the background color (or fill, like a javafx call)? Same for -fx-background-radius for corner radii and -fx-background-insets for insert?

Thanks for the help.

+3


source to share





All Articles