组件位置 (Vaadin)
我有一个简单的 portlet,包含几个组件:3 个 Button 对象、1 个 Slider、1 个 MenuBar 和分配给 Label 的图片(由 servlet 生成)。现在,当我在 Label
的图片之间切换时(我有更多图片),我希望将图片 Label
放置在旧图片 Label
处> 对象的位置:
我的图片Label
位于portlet 的左上角。当我选择另一张图片 时,
新图片 Button
对象、MenuBar
和 Slider
位于图片 Label
下方LabelLabel
正在其他组件下绘制(在 Button
对象、 MenuBar
、 Slider< /代码> )所以
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
rickthomas 是正确的,您应该使用 ReplaceComponent 方法。我很确定这里的主要问题是,在删除图片后,您调用 addComponent(pictureA) ,它实际上将组件添加到组件列表的末尾。如果您没有对旧图片的引用并且它是第一个组件,那么您可以使用这个:
除此之外,您不必编写 HTML 来显示图像。您可以使用嵌入式。
如果图像在您的类路径中,您可以使用以下命令:
如果在其他地方找到它们,请使用此命令:
这可能会解决您的问题。
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:
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:
If they are found somewhere else, use this:
This might solve your problem.
查看 Vaadin API javadoc,
我发现
我还没有测试它......但它应该可以工作。
Looking at the Vaadin API javadoc,
I found this
I haven't tested it... but it should work.