user9318046
user9318046

Reputation:

JavaFX Button Background Overflow 1px to the right

When I set a new Background to a Button, the Background has 1px more to the right, with the color that has it's opacity <1.

Is there a way to remove that 1px, or to fill it (opacity 1)? 1px isn't much, but if you place them next to each other, that 1px turn into a visible white space. The problem I saw it when the button is added into a Pane or Group, when a HBox or VBox is used that 1px gets filled. Unfortunately I need to use Pane or Group. I tried using custom Insets, but the problem still persists.

Code example:

Background background = new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY));
Font font = new Font("Times New Roman", 16);

Button a = new Button("A");
a.setFont(font);
a.setBackground(background);
a.setLayoutX(10);
a.setLayoutY(10);
pane.getChildren().add(a);

Button aa = new Button("AA");
aa.setFont(font);
aa.setBackground(background);
aa.setLayoutX(10);
aa.setLayoutY(45);
pane.getChildren().add(aa);

Button aaa = new Button("AAA");
aaa.setFont(font);
aaa.setBackground(background);
aaa.setLayoutX(10);
aaa.setLayoutY(80);
pane.getChildren().add(aaa);

Result (the image was zoomed in so that 1px is visible better):

How that 1px looks like (that 1px above the red lines):

How that 1px looks like

How that 1px looks like

How it looks like next to each other (you can see that space isn't white, nor green, but a light green:

How it looks like next to each other

How it looks like next to each other

Upvotes: 0

Views: 40

Answers (1)

fabian
fabian

Reputation: 82461

The effect observed is the result of the button width not being integral. This results in those parts not covering a pixel completely being rendered half-transparent.

Pane unfortunately does not consider the snapToPixel property and simply resizes the children to the sizes computed from it's properties.

To fix this, use a type of Pane that does snap coordinates to pixels, e.g. VBox or in this case a AnchorPane without any anchors set.

Pane pane = new AnchorPane();

Upvotes: 1

Related Questions