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.

layout-sub-window.asciidoc 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ---
  2. title: Sub-Windows
  3. order: 7
  4. layout: page
  5. ---
  6. [[layout.sub-window]]
  7. = Sub-Windows
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/structure/window"]
  11. endif::web[]
  12. __Sub-windows__ are floating panels within a native browser window. Unlike
  13. native browser windows, sub-windows are managed by the client-side runtime of
  14. Vaadin using HTML features. Vaadin allows opening, closing, resizing, maximizing
  15. and restoring sub-windows, as well as scrolling the window content.
  16. [[figure.layout.sub-window.basic]]
  17. .A Sub-Window
  18. image::img/subwindow-basic.png[width=50%, scaledwidth=70%]
  19. Sub-windows are typically used for __Dialog Windows__ and __Multiple Document
  20. Interface__ applications. Sub-windows are by default not modal; you can set them
  21. modal as described in <<layout.sub-window.modal>>.
  22. [[layout.sub-window.openclose]]
  23. == Opening and Closing Sub-Windows
  24. You can open a new sub-window by creating a new [classname]#Window# object and
  25. adding it to the UI with [methodname]#addWindow()#, typically in some event
  26. listener. A sub-window needs a content component, which is typically a layout.
  27. In the following, we display a sub-window immediately when a UI opens:
  28. [source, java]
  29. ----
  30. public static class SubWindowUI extends UI {
  31. @Override
  32. protected void init(VaadinRequest request) {
  33. // Some other UI content
  34. setContent(new Label("Here's my UI"));
  35. // Create a sub-window and set the content
  36. Window subWindow = new Window("Sub-window");
  37. VerticalLayout subContent = new VerticalLayout();
  38. subContent.setMargin(true);
  39. subWindow.setContent(subContent);
  40. // Put some components in it
  41. subContent.addComponent(new Label("Meatball sub"));
  42. subContent.addComponent(new Button("Awlright"));
  43. // Center it in the browser window
  44. subWindow.center();
  45. // Open it in the UI
  46. addWindow(subWindow);
  47. }
  48. }
  49. ----
  50. The result was shown in <<figure.layout.sub-window.basic>>. Sub-windows by
  51. default have undefined size in both dimensions, so they will shrink to fit the
  52. content.
  53. The user can close a sub-window by clicking the close button in the upper-right
  54. corner of the window. The button is controlled by the __closable__ property, so
  55. you can disable it with [methodname]#setClosable(false)#. You can also use keyboard
  56. shortcuts for closing a sub-window. You can manage the shortcuts with the [methodname]#addCloseShortcut()#,
  57. [methodname]#removeCloseShortcut()#, [methodname]#removeAllCloseShortcuts()#,
  58. [methodname]#hasCloseShortcut()#, and [methodname]#getCloseShortcuts()# methods.
  59. You close a sub-window also programmatically by calling the
  60. [methodname]#close()# for the sub-window, typically in a click listener for an
  61. [guibutton]#OK# or [guibutton]#Cancel# button. You can also call
  62. [methodname]#removeWindow()# for the current [classname]#UI#.
  63. ifdef::web[]
  64. [[layout.sub-window.openclose.example]]
  65. === Sub-Window Management
  66. Usually, you would extend the [classname]#Window# class for your specific
  67. sub-window as follows:
  68. [source, java]
  69. ----
  70. // Define a sub-window by inheritance
  71. class MySub extends Window {
  72. public MySub() {
  73. super("Subs on Sale"); // Set window caption
  74. center();
  75. // Some basic content for the window
  76. VerticalLayout content = new VerticalLayout();
  77. content.addComponent(new Label("Just say it's OK!"));
  78. content.setMargin(true);
  79. setContent(content);
  80. // Disable the close button
  81. setClosable(false);
  82. // Trivial logic for closing the sub-window
  83. Button ok = new Button("OK");
  84. ok.addClickListener(new ClickListener() {
  85. public void buttonClick(ClickEvent event) {
  86. close(); // Close the sub-window
  87. }
  88. });
  89. content.addComponent(ok);
  90. }
  91. }
  92. ----
  93. You could open the window as follows:
  94. [source, java]
  95. ----
  96. // Some UI logic to open the sub-window
  97. final Button open = new Button("Open Sub-Window");
  98. open.addClickListener(new ClickListener() {
  99. public void buttonClick(ClickEvent event) {
  100. MySub sub = new MySub();
  101. // Add it to the root component
  102. UI.getCurrent().addWindow(sub);
  103. }
  104. });
  105. ----
  106. endif::web[]
  107. [[layout.sub-window.position]]
  108. == Window Positioning
  109. When created, a sub-window will have an undefined default size and position. You
  110. can specify the size of a window with [methodname]#setHeight()# and
  111. [methodname]#setWidth()# methods. You can set the position of the window with
  112. [methodname]#setPositionX()# and [methodname]#setPositionY()# methods.
  113. [source, java]
  114. ----
  115. // Create a new sub-window
  116. mywindow = new Window("My Dialog");
  117. // Set window size.
  118. mywindow.setHeight("200px");
  119. mywindow.setWidth("400px");
  120. // Set window position.
  121. mywindow.setPositionX(200);
  122. mywindow.setPositionY(50);
  123. UI.getCurrent().addWindow(mywindow);
  124. ----
  125. [[layout.sub-window.scrolling]]
  126. == Scrolling Sub-Window Content
  127. ((("scroll bars", id="term.layout.sub-window.scrolling.scrollbars", range="startofrange")))
  128. If a sub-window has a fixed or percentual size and its content becomes too big
  129. to fit in the content area, a scroll bar will appear for the particular
  130. direction. On the other hand, if the sub-window has undefined size in the
  131. direction, it will fit the size of the content and never get a scroll bar.
  132. Scroll bars in sub-windows are handled with regular HTML features, namely
  133. [literal]#++overflow: auto++# property in CSS.
  134. ((("overflow")))
  135. ((("[interfacename]#Scrollable#")))
  136. As [classname]#Window# extends [classname]#Panel#, windows are also
  137. [interfacename]#Scrollable#. Note that the interface defines __programmatic
  138. scrolling__, not scrolling by the user. Please see
  139. <<dummy/../../../framework/layout/layout-panel#layout.panel,"Panel">>.
  140. (((range="endofrange", startref="term.layout.sub-window.scrolling.scrollbars")))
  141. [[layout.sub-window.modal]]
  142. == Modal Sub-Windows
  143. A modal window is a sub-window that prevents interaction with the other UI.
  144. Dialog windows, as illustrated in <<figure.layout.sub-window.modal>>, are
  145. typical cases of modal windows. The advantage of modal windows is limiting the
  146. scope of user interaction to a sub-task, so changes in application state are
  147. more limited. The disadvantage of modal windows is that they can restrict
  148. workflow too much.
  149. You can make a sub-window modal with [methodname]#setModal(true)#.
  150. [[figure.layout.sub-window.modal]]
  151. .Modal Sub-Window
  152. image::img/subwindow-modal.png[width=70%, scaledwidth=100%]
  153. Depending on the theme, the parent window may be grayed when the modal window is
  154. open.
  155. [WARNING]
  156. .Security Warning
  157. ====
  158. Modality of child windows is purely a client-side feature and can be
  159. circumvented with client-side attack code. You should not trust in the modality
  160. of child windows in security-critical situations such as login windows.
  161. ====