Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx: how make a clickable image using scenebuilder [closed]

I wanted to know how to make clickable image using ImageView which takes me to another FXML using scene builder. I am using eclipse IDE.

like image 605
mistletoe Avatar asked Jan 05 '23 02:01

mistletoe


1 Answers

I've never used scene builder, but you can just call setOnMouseClicked(EventHandler<MouseEvent> event) on the ImageView object.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ImageClickExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        ImageView img = new ImageView("http://i.stack.imgur.com/oURrw.png");
        img.setPickOnBounds(true); // allows click on transparent areas
        img.setOnMouseClicked((MouseEvent e) -> {
            System.out.println("Clicked!"); // change functionality
        });
        Scene scene = new Scene(new StackPane(img));
        primaryStage.setTitle("Image Click Example");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
like image 173
d.j.brown Avatar answered Jan 07 '23 16:01

d.j.brown