开发者

Component Position (Vaadin)

开发者 https://www.devze.com 2023-04-04 05:46 出处:网络
I have a simple portlet with a few components : 3 Button objects, 1 Slider, 1 MenuBar and a picture assigned to a Label (generated by servlet). Now when I switch between pictures for a Label (I have m

I have a simple portlet with a few components : 3 Button objects, 1 Slider, 1 MenuBar and a picture assigned to a Label (generated by servlet). Now when I switch between pictures for a Label (I have more of them), I want the picture Label to be placed at the old picture Label object's position:

My picture Label is in the left corner of the portlet. The Button objects, MenuBar, and the Slider are under the picture Label when I select another picture Label the new picture Label is being drawn under the other components (under the Button objects , MenuBar , Slider ) so the Button objects... are top and the picture Label is at the bottom of the portlet

for example, I change the background of the picture Label by selecting the color in the menu :

newItem1.addItem("Blue",new Command(){
    public void menuSelected(MenuItem selectedItem){
        if(pictureA.isVisible()){
            pictureB.setVisible(false);
            pictureC.setVisible(false);
            window.removeComponent(pictureA);
            pictureA= new Label("<img src=http://localhost:8888/portlet/KiviatDiagramm?background=blue", Label.CONTENT_XHTML);
            window.addComponent(pictureA);
        } else {
            window.showNotification("", Notification.TYPE_WARNING_MESSAGE);

        }
    }
});

UPDATE :

I have switched from Label objects to embedded images (Embedded) (which is a lot better) I have tried to reassign the resource on the Embedded object with the new color but it doesn't work, here is what I've done :

public void init() {

    URL PictureAUrl= null;
    try {
        pictureAUrl= new URL("http://localhost:8888/portlet/pictureA");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URL PictureBUrl= null;
    try {
        pictureAUrl= new URL("http://localhost:8888/portlet/pictureB");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URL pictureCUrl= null;
    try {
        pictureCUrl= new URL("http://localhost:8888/portlet/pictureC");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    final Embedded pictureA = new Embedded("", new ExternalResource(pictureAURL));
    pictureA .setType(Embedded.TYPE_IMAGE);
    final Embedded pictureB = new Embedded("", new ExternalResource(pictureBURL));
    pictureB .setType(Embedded.TYPE_IMAGE);
    final Embedded pictureC = new Embedded("", new ExternalResource(pictureCURL));
    pictureC .setType(Embedded.TYPE_IMAGE);


    newItem.addItem("ColorBlue", new Command(){
            public void menuSelected(MenuItem selectedItem) {
                if(!pictureA.equals(pictu开发者_如何学GoreB )){

        Resource newPictureResource = new ExternalResource("http://localhost:8888/portlet/pictureA?background=blue");
        newPictureResource.setType(Embedded.TYPE_IMAGE);
        pictureA.setResource(newPictureResource);

                }
                else {
                    window.showNotification("Please select pictureA");
                }
        }
   });


rickthomas is correct, you should use the replaceComponent method. I'm pretty sure that the main problem here is that after you have removed the picture, you call addComponent(pictureA) which actually adds the component to the end of the component list. If you don't have a reference to the old picture and it's the first component, then you can use this:

window.replaceComponent(window.getComponentIterator().next(), newPicture);

In addition to that, you don't have to write HTML to show images. You can use Embedded.

If the images are in your classpath, you can use the following:

Embedded newPicture = new Embedded("", new ClassResource("my-picture.png", myApplication));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);

If they are found somewhere else, use this:

URL url = new URL("http://localhost:8888/portlet/KiviatDiagramm?background=blue");
Embedded newPicture = new Embedded("", new ExternalResource(url));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);

This might solve your problem.


Looking at the Vaadin API javadoc,

I found this

public void replaceComponent(Component oldComponent,Component newComponent)

I haven't tested it... but it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消