You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-embedded.asciidoc 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ---
  2. title: Embedded Resources
  3. order: 33
  4. layout: page
  5. ---
  6. [[components.embedded]]
  7. = Embedded Resources
  8. You can embed images in Vaadin UIs with the [classname]#Image# component, Adobe
  9. Flash graphics with [classname]#Flash#, and other web content with
  10. [classname]#BrowserFrame#. There is also a generic [classname]#Embedded#
  11. component for embedding other object types.
  12. The embedded content is referenced as _resources_, as described in <<dummy/../../../framework/application/application-resources#application.resources,"Images and Other Resources">>.
  13. The following example displays an image as a class resource loaded with the
  14. class loader:
  15. [source, java]
  16. ----
  17. Image image = new Image("Yes, logo:",
  18. new ClassResource("vaadin-logo.png"));
  19. main.addComponent(image);
  20. ----
  21. The caption can be given as null to disable it. An empty string displays an
  22. empty caption which takes a bit space. The caption is managed by the containing
  23. layout.
  24. You can set an alternative text for an embedded resource with
  25. [methodname]#setAlternateText()#, which can be shown if images are disabled in
  26. the browser for some reason. The text can be used for accessibility purposes,
  27. such as for text-to-speech generation.
  28. [[components.embedded.image]]
  29. == Embedded [classname]#Image#
  30. The [classname]#Image# component allows embedding an image resource in a Vaadin
  31. UI.
  32. [source, java]
  33. ----
  34. // Serve the image from the theme
  35. Resource res = new ThemeResource("img/myimage.png");
  36. // Display the image without caption
  37. Image image = new Image(null, res);
  38. layout.addComponent(image);
  39. ----
  40. The [classname]#Image# component has by default undefined size in both
  41. directions, so it will automatically fit the size of the embedded image.
  42. ((("scroll
  43. bars")))
  44. If you want scrolling with scroll bars, you can put the image inside a
  45. [classname]#Panel# that has a defined size to enable scrolling, as described in
  46. <<dummy/../../../framework/layout/layout-panel#layout.panel.scrolling,"Scrolling
  47. the Panel Content">>. You can also put it inside some other component container
  48. and set the [literal]#++overflow: auto++# CSS property for the container element
  49. in a theme to enable automatic scrollbars. (((overflow CSS
  50. property)))
  51. [[components.embedded.image.generated]]
  52. === Generating and Reloading Images
  53. You can also generate the image content dynamically using a
  54. [classname]#StreamResource#, as described in
  55. <<dummy/../../../framework/application/application-resources#application.resources.stream,"Stream
  56. Resources">>, or with a [classname]#RequestHandler#.
  57. If the image changes, the browser needs to reload it. Simply updating the stream
  58. resource is not enough. Because of how caching is handled in some browsers, you
  59. can cause a reload easiest by renaming the filename of the resource with a
  60. unique name, such as one including a timestamp. You should set cache time to
  61. zero with [methodname]#setCacheTime()# for the resource object when you create
  62. it.
  63. // BUG #2470.
  64. [source, java]
  65. ----
  66. // Create the stream resource with some initial filename
  67. StreamResource imageResource =
  68. new StreamResource(imageSource, "initial-filename.png");
  69. // Instruct browser not to cache the image
  70. imageResource.setCacheTime(0);
  71. // Display the image
  72. Image image = new Image(null, imageResource);
  73. ----
  74. When refreshing, you also need to call [methodname]#markAsDirty()# for the
  75. [classname]#Image# object.
  76. [source, java]
  77. ----
  78. // This needs to be done, but is not sufficient
  79. image.markAsDirty();
  80. // Generate a filename with a timestamp
  81. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  82. String filename = "myfilename-" + df.format(new Date()) + ".png";
  83. // Replace the filename in the resource
  84. imageResource.setFilename(makeImageFilename());
  85. ----
  86. [[components.embedded.flash]]
  87. == Adobe [classname]#Flash# Graphics
  88. The [classname]#Flash# component allows embedding Adobe Flash animations in
  89. Vaadin UIs.
  90. [source, java]
  91. ----
  92. Flash flash = new Flash(null,
  93. new ThemeResource("img/vaadin_spin.swf"));
  94. layout.addComponent(flash);
  95. ----
  96. You can set Flash parameters with [methodname]#setParameter()#, which takes a
  97. parameter's name and value as strings. You can also set the
  98. [parameter]#codeBase#, [parameter]#archive#, and [parameter]#standBy# attributes
  99. for the Flash object element in HTML.
  100. [[components.embedded.browserframe]]
  101. == [classname]#BrowserFrame#
  102. The [classname]#BrowserFrame# allows embedding web content inside an HTML
  103. `&lt;iframe&gt;` element. You can refer to an external URL with
  104. [classname]#ExternalResource#.
  105. As the [classname]#BrowserFrame# has undefined size by default, it is critical
  106. that you define a meaningful size for it, either fixed or relative.
  107. [source, java]
  108. ----
  109. BrowserFrame browser = new BrowserFrame("Browser",
  110. new ExternalResource("http://demo.vaadin.com/sampler/"));
  111. browser.setWidth("600px");
  112. browser.setHeight("400px");
  113. layout.addComponent(browser);
  114. ----
  115. Notice that web pages can prevent embedding them in an &lt;iframe&gt;.
  116. [[components.embedded.embedded]]
  117. == Generic [classname]#Embedded# Objects
  118. The generic [classname]#Embedded# component allows embedding all sorts of
  119. objects, such as SVG graphics, Java applets, and PDF documents, in addition to
  120. the images, Flash graphics, and browser frames which you can embed with the
  121. specialized components.
  122. For example, to display a Flash animation:
  123. [source, java]
  124. ----
  125. // A resource reference to some object
  126. Resource res = new ThemeResource("img/vaadin_spin.swf");
  127. // Display the object
  128. Embedded object = new Embedded("My Object", res);
  129. layout.addComponent(object);
  130. ----
  131. Or an SVG image:
  132. [source, java]
  133. ----
  134. // A resource reference to some object
  135. Resource res = new ThemeResource("img/reindeer.svg");
  136. // Display the object
  137. Embedded object = new Embedded("My SVG", res);
  138. object.setMimeType("image/svg+xml"); // Unnecessary
  139. layout.addComponent(object);
  140. ----
  141. The MIME type of the objects is usually detected automatically from the filename
  142. extension with the [classname]#FileTypeResolver# utility in Vaadin. If not, you
  143. can set it explicitly with [methodname]#setMimeType()#, as was done in the
  144. example above (where it was actually unnecessary).
  145. Some embeddable object types may require special support in the browser. You
  146. should make sure that there is a proper fallback mechanism if the browser does
  147. not support the embedded type.