I'm creating a Java Application which lets the User click a button that opens a File Selector and upon selecting an image creates a Button with that image and text from a Text Field. It's working without the image but I can't figure out how to add an image to the button for the life of me.
Context: btns is a GridPane of Buttons, lastTopIn is an integer that keeps track of the last used column of the GridPane. The button removes itself when clicked.
FileChooser fc = new FileChooser()
fc.setTitle("Choose Image for Button");
File file = fc.showOpenDialog(null);
lastTopIn++;
Button thebutton = new Button((String) tf.getText(), new ImageView(new Image(getClass().getResourceAsStream("file://"+file.getAbsolutePath()))));
thebutton.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event event) {
btns.getChildren().remove(thebutton);
}
});
btns.add(thebutton, lastTopIn,1);
Thanks in advance for any help.
getClass().getResourceAsStream("file://"+file.getAbsolutePath()). Not 100% sure though.setOnMouseClickedon a button; usesetOnActioninstead.