how to load a svg in gwt widget from an url

后端 未结 2 1693
刺人心
刺人心 2021-01-24 09:05

I have a servlet that delivers png and svg images. With png i have no problem:

Image image = new Image(GWT.getHostPageBaseURL() + token);

But h

相关标签:
2条回答
  • 2021-01-24 09:55

    If you use a URL to load an image, you don't need any libraries at all. And you don't need a servlet to deliver them. Just add images to your /war/images folder. Then, in your GWT code:

    Image image = new Image();
    image.setUrl("images/myImage.svg");
    myPanel.add(image);
    

    You may want to add some logic for browsers that do not support svg files.

    0 讨论(0)
  • 2021-01-24 09:55

    I found a solution that works without any external library (like Andrei's Solution) but keeps also the embedded scripts working. I used the info from here - i used a HTMLPanel and loaded the image via "RequestBuilder":

        String url = GWT.getHostPageBaseURL() + link.getToken();
        RequestBuilder rB = new RequestBuilder(RequestBuilder.GET, url);
        rB.setCallback(new RequestCallback() {
    
            @Override
            public void onResponseReceived(Request request, Response response) {
                //create Widget
                chartImage = new HTMLPanel(response.getText());
                //add to layout   
                layout.add(chartImage);
    
            }
    
            @Override
            public void onError(Request request, Throwable exception) {
                // TODO Auto-generated method stub
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题