How to change button image in javaFX?

I am using javaFX. I made a button and set the image for this. the code:

    Image playI=new Image("file:///c:/Users/Farhad/Desktop/icons/play2.jpg");
    ImageView iv1=new ImageView(playI);
    iv1.setFitHeight(67);
    iv1.setFitWidth(69);

    Button playB=new Button("",iv1);

But I want, when I press the button, the image will change to another image. How can i do this?

+4
source share
1 answer

You can set the Graphic buttons in Action

Image image = new Image(getClass().getResourceAsStream("play3.jpg"));
button.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        Button button = (Button) e.getSource();
        button.setGraphic(new ImageView(image));
    }
});
+7
source

Source: https://habr.com/ru/post/1598540/


All Articles