Hayk Harutyunyan
Hayk Harutyunyan

Reputation: 117

how open image with url in javafx

I have banner an I wont to put it in my javaFX application. And when user click on the image open default browser.

try {
            String path = "http://developer.am/webservice/banner728x90.gif";
            URL url = new URL(path);
            BufferedImage image = ImageIO.read(url);
            label = new JLabel(new ImageIcon(image));

        } catch (Exception exp) {
            exp.printStackTrace();
        }

also I am trying to convert above code from awt in JavaFX

Upvotes: 1

Views: 16790

Answers (1)

eckig
eckig

Reputation: 11134

Lets see. First the ingredients:

  1. Image
  2. Button
  3. ImageView
  4. Open Link in System Browser with JavaFX

Putting this together:

String path = "http://...";
String pathToOpen = "http://...";

Image image = new Image(path);
ImageView imageView = new ImageView(image);

Button button = new Button("clickMe!", imageView);
button.setOnAction(ev -> getHostServices().showDocument(pathToOpen));

Upvotes: 14

Related Questions