Home » Java » Setting Image on Button in JavaFX

Setting Image on Button in JavaFX

Posted by: admin June 14, 2018 Leave a comment

Questions:

Maybe this question is duplicate, but I have looked into other posts about this topic and tried solutions from there, but I’m still stuck and my code won’t work. How can I set an Image on a Button?

The Application is showing a button with nothing on it.

Image image = new Image("file:~/IdeaProjects/Memory/Images/eichhoernchen.jpg");
ImageView iv = new ImageView(image);
Button root = new Button();
root.setGraphic(iv);
Scene s = new Scene(root, 300, 300);
primaryStage.setScene(s);
primaryStage.show();
Answers: