捕获URL图像JavaFX

时间:2015-09-24 08:03:40

标签: image url webview javafx

我有一个webview,在那里我查看来自研究的图像(使用自定义搜索API),通过点击其中一个(一次或两次无关紧要,我需要理解这个概念)我应该能够捕获他的网址和hten关闭webview。目前,Url图像是静态的并且使用ArrayList进行管理,因为我在另一个类中进行了研究,下一步和最后一步是将webview集成到Jframe中,但这将是另一个问题。那么,我怎样才能获得该网址?

这里是代码:

import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class WebViewSample extends Application {
    private Scene scene;
    @Override public void start(Stage stage) {
        // create the scene
        stage.setTitle("Web View");
        scene = new Scene(new Browser(),750,500, Color.web("#666970"));
        stage.setScene(scene);
        //scene.getStylesheets().add("webviewsample/BrowserToolbar.css"); 

        stage.show();

//        scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent mouseEvent) {
//                System.out.println("mouse click detected! " + mouseEvent.getSource());
//            }
//        });
    }

    public static void main(String[] args){
        launch(args);
    }
}
class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        //apply the styles
        getStyleClass().add("browser");
        // load the web page
        int i=0;
        //webEngine.load("https://www.google.it/earth/images/products_mobile.png");

        String first ="<html> <body>";
        String fin ="</body>  </html>";
        String complete = "";


        ArrayList<String> test = new ArrayList<>();
        test.add("http://www.google.it/events/images/speakers_global/marco_polillo.jpg");
        test.add("https://www.google.it/earth/images/products_mobile.png");



        complete = complete.concat(first);
        for(int j = 0; j< test.size(); j++)
        {

            complete = complete.concat("<img src=");
            complete = complete.concat(test.get(j));
            complete = complete.concat(" style= padding:10px; margin:20px; display:inline-block; margin: 0px 12px 12px 0px; overflow: ; position: relative; vertical-align: top; z-index: 5; height:200px width:auto border:6px solid red>");



        }


        complete = complete.concat(fin);

        browser.getEngine().loadContent(complete);


        //add the web view to the scene
        getChildren().add(browser);


    }
    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
    }

    @Override protected double computePrefWidth(double height) {
        return 750;
    }

    @Override protected double computePrefHeight(double width) {
        return 500;
    }




}

1 个答案:

答案 0 :(得分:0)

单击图像会在WebPage本身中发生,并且您不会在JavaFX部件中发生任何单击事件,因此您可以处理它。您可以做的是拥有一个JavaScript函数,在单击图像时调用Java代码中的方法。在JavaScript中,您可以完全访问文档,并可以确定单击了哪个图像。

以下是建立桥梁所需的部分:

public class Bridge{
    WebEngine engine;    // add a new field

    public Bridge(WebEngine engine){
        this.engine = engine;
    }
    public void clickedOnImage(String imageUrl) {
        // TODO fill in your logic
    }

}

// In your Browser class Constructor
JSObject jsobj = (JSObject) engine.executeScript("window");
jsobj.setMember("java", new Bridge(engine));   // pass it here

在JavaScript代码中,您可以调用此Java方法:

var imgSrc = ...
java.clickedOnImage(imgSrc);