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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ---
  2. title: Embedded Resources
  3. order: 34
  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.browserframe]]
  87. == [classname]#BrowserFrame#
  88. The [classname]#BrowserFrame# allows embedding web content inside an HTML
  89. `&lt;iframe&gt;` element. You can refer to an external URL with
  90. [classname]#ExternalResource#.
  91. As the [classname]#BrowserFrame# has undefined size by default, it is critical
  92. that you define a meaningful size for it, either fixed or relative.
  93. [source, java]
  94. ----
  95. BrowserFrame browser = new BrowserFrame("Browser",
  96. new ExternalResource("http://demo.vaadin.com/sampler/"));
  97. browser.setWidth("600px");
  98. browser.setHeight("400px");
  99. layout.addComponent(browser);
  100. ----
  101. Notice that web pages can prevent embedding them in an &lt;iframe&gt;.
  102. [[components.embedded.embedded]]
  103. == Generic [classname]#Embedded# Objects
  104. The generic [classname]#Embedded# component allows embedding all sorts of
  105. objects, such as SVG graphics, Java applets, and PDF documents, in addition to
  106. the images, and browser frames which you can embed with the
  107. specialized components.
  108. Display an SVG image:
  109. [source, java]
  110. ----
  111. // A resource reference to some object
  112. Resource res = new ThemeResource("img/reindeer.svg");
  113. // Display the object
  114. Embedded object = new Embedded("My SVG", res);
  115. object.setMimeType("image/svg+xml"); // Unnecessary
  116. layout.addComponent(object);
  117. ----
  118. The MIME type of the objects is usually detected automatically from the filename
  119. extension with the [classname]#FileTypeResolver# utility in Framework. If not, you
  120. can set it explicitly with [methodname]#setMimeType()#, as was done in the
  121. example above (where it was actually unnecessary).
  122. Some embeddable object types may require special support in the browser. You
  123. should make sure that there is a proper fallback mechanism if the browser does
  124. not support the embedded type.