Resize a Button JavaFX


Question

i have a question about a JavaFx Button. In the following code i add a Button into a HBox.

ivTriangleImg.setFitHeight(16);
ivTriangleImg.setFitWidth(16);
ivTriangleImg.setRotate(iRotateCoord1);
btnTriangle.setGraphic(ivTriangleImg);
btnTriangle.setStyle("-fx-background-color:green;");
addComponentToBox(btnTriangle);

The Button gets a Graphic -> The Graphic is a transparent triangle with the Size of 16x16 pixels. The Problem is, that the Button doesn't have the Size 16x16 it is so much more. How can i get the Button smaller and the Pictur must have the same size?

1
0
10/20/2014 7:02:34 AM

Accepted Answer

For JavaFX 8 use

btnTriangle.setPadding(Insets.EMPTY);

For JavaFX 2 use

btnTriangle.setStyle("-fx-padding: 0;");

However you can directly put the image view to the scene rather than setting it to the button's graphic, and add listeners you want:

ivTriangleImg.setOnMouseClicked(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {
        // do something
    }
});
2
10/20/2014 7:35:05 AM

I added your Code, it works very fine. I made it a little bit different:

        btnTriangle.setStyle("-fx-background-color:transparent;-fx-padding:0;-fx-background-size:0;");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon