组件位置 (Vaadin)

发布于 2024-12-03 21:11:05 字数 2823 浏览 1 评论 0原文

我有一个简单的 portlet,包含几个组件:3 个 Button 对象、1 个 Slider、1 个 MenuBar 和分配给 Label 的图片(由 servlet 生成)。现在,当我在 Label 的图片之间切换时(我有更多图片),我希望将图片 Label 放置在旧图片 Label 处> 对象的位置:

我的图片Label 位于portlet 的左上角。当我选择另一张图片 时,Button 对象、MenuBarSlider 位于图片 Label 下方Label 新图片 Label 正在其他组件下绘制(在 Button 对象、 MenuBarSlider< /代码> )所以Button 对象...位于顶部,图片 Label 位于 portlet 的底部,

例如,我更改图片 Label 的背景通过在菜单中选择颜色:

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);

        }
    }
});

更新:

我已从Label对象切换为嵌入图像(Embedded)(这要好得多)我尝试重新分配资源具有新颜色的嵌入对象,但它不起作用,这是我所做的:

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(pictureB )){

        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");
                }
        }
   });

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(pictureB )){

        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");
                }
        }
   });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森末i 2024-12-10 21:11:05

rickthomas 是正确的,您应该使用 ReplaceComponent 方法。我很确定这里的主要问题是,在删除图片后,您调用 addComponent(pictureA) ,它实际上将组件添加到组件列表的末尾。如果您没有对旧图片的引用并且它是第一个组件,那么您可以使用这个:

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

除此之外,您不必编写 HTML 来显示图像。您可以使用嵌入式

如果图像在您的类路径中,您可以使用以下命令:

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

如果在其他地方找到它们,请使用此命令:

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);

这可能会解决您的问题。

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.

段念尘 2024-12-10 21:11:05

查看 Vaadin API javadoc,

我发现

public void replaceComponent(Component oldComponent,Component newComponent)

我还没有测试它......但它应该可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文