Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using SVG images in Buttons

bjbcatsNov 2 2014

I found this SVG to FXML converter SVG To FXML Converter as Commandline util | Tomsondev Blog which works really well.

In my code I have:

@FXML

private Button demobutton;

FXMLLoader loader = new FXMLLoader(getClass().getResource("tiger.fxml"));

Parent root = (Parent) loader.load();

Then I'd like to set the image in the button, something like

demobutton.setGraphic(root);

demobutton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

If I do this, the button is sized the same as the size of the image, which is fine if that's what you want, but what I'd really like to do is scale the image so it fits the default layout size of the button. Simply scaling the image makes the image smaller, but the button is still sized the same as the original image. The only way I've found to make it work is:

double xx = root.getBoundsInLocal().getWidth();

double yy = root.getBoundsInLocal().getHeight();

final Button button = new Button("Some Text");

final Scene snapScene = new Scene(button);

snapScene.snapshot(null);

double w = button.getWidth();

double h = button.getHeight();

root.setScaleX((h-4.0) / xx);

root.setScaleY((h-4.0) / yy);

demobutton.setMaxHeight(h);

demobutton.setMaxWidth(w);

demobutton.setMinHeight(h);

demobutton.setMinWidth(w);

demobutton.setPrefWidth(w);

demobutton.setPrefHeight(h);

demobutton.setGraphic(root);

demobutton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

which seems a very horrible way to do it. Is there a better means to do this ?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 30 2014
Added on Nov 2 2014
0 comments
842 views