Vaadin Web应用开发教程(21):UI组件-Embedded组件
Embedded组件支持者浏览器中嵌入媒体对象,如图像,动画等其它浏览器支持的媒体类型。Embedded组件内容在Vaadin中是作为资源来管理的
[java]
Embedded image = new Embedded("Yes, logo:",
new ClassResource("vaadin-logo.png", this));
main.addComponent(image);
Embedded image = new Embedded("Yes, logo:",
new ClassResource("vaadin-logo.png", this));
main.addComponent(image);
The Embedded.TYPE_OBJECT 支持浏览器嵌入媒体,目前只支持显示Flas易做图,它的MIME类型为application/x-shockwave-flash.
[java]
// Create a Shockware Flash resource
final ClassResource flashResource =
new ClassResource("itmill_spin.swf", getApplication());
// Display the resource in a Embedded compoant
final Embedded embedded =
new Embedded("Embedded Caption", flashResource);
// This is the default type, but we set it anyway.
embedded.setType(Embedded.TYPE_OBJECT);
// This is recorgnized automatically, but set it anyway.
embedded.setMimeType("application/x-shockwave-flash");
// Create a Shockware Flash resource
final ClassResource flashResource =
new ClassResource("itmill_spin.swf", getApplication());
// Display the resource in a Embedded compoant
final Embedded embedded =
new Embedded("Embedded Caption", flashResource);
// This is the default type, but we set it anyway.
embedded.setType(Embedded.TYPE_OBJECT);
// This is recorgnized automatically, but set it anyway.
embedded.setMimeType("application/x-shockwave-flash");
可以通过方法setParameter 为对象设置参数。
Embedded.TYPE_IMAGE 用来显示图像,通常无需明确指定其类型。 Embedded 组件缺省未定义宽度和高度,因此可以自动适应所显示图像的大小,如果需要使用滚动条,可以在Panel中嵌入Embedded组件。
如果需要显示动态生成的图像,比如从StreamResource显示图像并且显示对象发生变化,就需要在浏览器重新加载图像,不同浏览器处理缓存Cache的方法不同,因此保险的方法是为动态生成的图像使用不同的文件名,
并在创建图像使用setCacheTime 将Cache时间设为0.
[java]
// Create the stream resource with some initial filename.
StreamResource imageResource =
new StreamResource(imageSource, "initial-filename.png",
getApplication());
// Instruct browser not to cache the image.
imageResource.setCacheTime(0);
// Display the image in an Embedded component.
Embedded embedded = new Embedded("", imageResource);
// Create the stream resource with some initial filename.
StreamResource imageResource =
new StreamResource(imageSource, "initial-filename.png",
getApplication());
// Instruct browser not to cache the image.
imageResource.setCacheTime(0);
// Display the image in an Embedded component.
Embedded embedded = new Embedded("", imageResource);
刷新图像使用requestRepaint() 方法。
[java]
// This needs to be done, but is not sufficient.
embedded.requestRepaint();
// Generate a filename with a timestamp.
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String filename = "myfilename-" + df.format(new Date()) + ".png";
// Replace the filename in the resource.
imageResource.setFilename(makeImageFilename());
// This needs to be done, but is not sufficient.
embedded.requestRepaint();
// Generate a filename with a timestamp.
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String filename = "myfilename-" + df.format(new Date()) + ".png";
// Replace the filename in the resource.
imageResource.setFilename(makeImageFilename());
Embedded.TYPE_BROWSER在iframe中显示一个外部链接。
[java]
URL url = new URL("http://dev.vaadin.com/");
Embedded browser = new Embedded("", new ExternalResource(url));
browser.setType(Embedded.TYPE_BROWSER);
main.addComponent(browser);
URL url = new URL("http://dev.vaadin.com/");
Embedded browser = new Embedded("", new ExternalResource(url));
browser.setType(Embedded.TYPE_BROWSER);
main.addComponent(browser);
补充:Web开发 , 其他 ,