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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. subWindow.setContent(subContent);
  39. // Put some components in it
  40. subContent.addComponent(new Label("Meatball sub"));
  41. subContent.addComponent(new Button("Awlright"));
  42. // Center it in the browser window
  43. subWindow.center();
  44. // Open it in the UI
  45. addWindow(subWindow);
  46. }
  47. }
  48. ----
  49. The result was shown in <<figure.layout.sub-window.basic>>. Sub-windows by
  50. default have undefined size in both dimensions, so they will shrink to fit the
  51. content.
  52. The user can close a sub-window by clicking the close button in the upper-right
  53. corner of the window. The button is controlled by the __closable__ property, so
  54. you can disable it with [methodname]#setClosable(false)#. You can also use keyboard
  55. shortcuts for closing a sub-window. You can manage the shortcuts with the [methodname]#addCloseShortcut()#,
  56. [methodname]#removeCloseShortcut()#, [methodname]#removeAllCloseShortcuts()#,
  57. [methodname]#hasCloseShortcut()#, and [methodname]#getCloseShortcuts()# methods.
  58. You close a sub-window also programmatically by calling the
  59. [methodname]#close()# for the sub-window, typically in a click listener for an
  60. [guibutton]#OK# or [guibutton]#Cancel# button. You can also call
  61. [methodname]#removeWindow()# for the current [classname]#UI#.
  62. [[layout.sub-window.openclose.example]]
  63. === Sub-Window Management
  64. Usually, you would extend the [classname]#Window# class for your specific
  65. sub-window as follows:
  66. [source, java]
  67. ----
  68. // Define a sub-window by inheritance
  69. class MySub extends Window {
  70. public MySub() {
  71. super("Subs on Sale"); // Set window caption
  72. center();
  73. // Disable the close button
  74. setClosable(false);
  75. setContent(new Button("Close me", event -> close()));
  76. }
  77. }
  78. ----
  79. You could open the window as follows:
  80. [source, java]
  81. ----
  82. // Some UI logic to open the sub-window
  83. final Button open = new Button("Open Sub-Window");
  84. open.addClickListener(event -> {
  85. MySub sub = new MySub();
  86. // Add it to the root component
  87. UI.getCurrent().addWindow(sub);
  88. });
  89. ----
  90. [[layout.sub-window.position]]
  91. == Window Positioning
  92. When created, a sub-window will have an undefined default size and position. You
  93. can specify the size of a window with [methodname]#setHeight()# and
  94. [methodname]#setWidth()# methods. You can set the position of the window with
  95. [methodname]#setPositionX()# and [methodname]#setPositionY()# methods.
  96. [source, java]
  97. ----
  98. // Create a new sub-window
  99. mywindow = new Window("My Dialog");
  100. // Set window size.
  101. mywindow.setHeight("200px");
  102. mywindow.setWidth("400px");
  103. // Set window position.
  104. mywindow.setPositionX(200);
  105. mywindow.setPositionY(50);
  106. UI.getCurrent().addWindow(mywindow);
  107. ----
  108. [[layout.sub-window.scrolling]]
  109. == Scrolling Sub-Window Content
  110. ((("scroll bars", id="term.layout.sub-window.scrolling.scrollbars", range="startofrange")))
  111. If a sub-window has a fixed or percentual size and its content becomes too big
  112. to fit in the content area, a scroll bar will appear for the particular
  113. direction. On the other hand, if the sub-window has undefined size in the
  114. direction, it will fit the size of the content and never get a scroll bar.
  115. Scroll bars in sub-windows are handled with regular HTML features, namely
  116. [literal]#++overflow: auto++# property in CSS.
  117. ((("overflow")))
  118. ((("[interfacename]#Scrollable#")))
  119. As [classname]#Window# extends [classname]#Panel#, windows are also
  120. [interfacename]#Scrollable#. Note that the interface defines __programmatic
  121. scrolling__, not scrolling by the user. Please see
  122. <<layout-panel#layout.panel,"Panel">>.
  123. (((range="endofrange", startref="term.layout.sub-window.scrolling.scrollbars")))
  124. [[layout.sub-window.modal]]
  125. == Modal Sub-Windows
  126. A modal window is a sub-window that prevents interaction with the other UI.
  127. Dialog windows, as illustrated in <<figure.layout.sub-window.modal>>, are
  128. typical cases of modal windows. The advantage of modal windows is limiting the
  129. scope of user interaction to a sub-task, so changes in application state are
  130. more limited. The disadvantage of modal windows is that they can restrict
  131. workflow too much.
  132. You can make a sub-window modal with [methodname]#setModal(true)#.
  133. [[figure.layout.sub-window.modal]]
  134. .Modal Sub-Window
  135. image::img/subwindow-modal.png[width=70%, scaledwidth=100%]
  136. Depending on the theme, the parent window may be grayed when the modal window is
  137. open.
  138. [WARNING]
  139. .Security Warning
  140. ====
  141. Modality of child windows is purely a client-side feature and can be
  142. circumvented with client-side attack code. You should not trust in the modality
  143. of child windows in security-critical situations such as login windows.
  144. ====