WARNING: This tutorial is being written! Do not hesitate to report any errors or suggestions.
Quick access:
The different layout
Example of use of the layout
As you already noticed, the object model of the JWT library (JavaScript Widget Toolkit) draws heavily on the Java Swing library.
This is the same for the positioning logic of the components within the JWT containers: layouts can be injected
in a container to control the positioning of its components. Like Java, several layout classes are offered: they derive
all of the class corelib.services.web.javascript.jwt.LayoutManager.
In the present state of things, three layout are proposed to you: the strategy FlowLayout, the strategy
VerticalLayout and the strategy BorderLayout. Consider one to one of these different layout.
corelib.services.web.javascript.jwt.FlowLayout: this strategy seeks the positioning of the elements the one behind the
other horizontally. If there is more space in the container, the following elements will be placed on the following lines. This
strategy seeks to resize the elements to their size as small as possible. Finally you can note that the elements, of the same line, can be
aligned to the left, centered or aligned to the right. This strategy is especially used to generate horizontally menu bars.
Attention!: this layout is almost the same in Java (AWT
and Swing).
corelib.services.web.javascript.jwt.VerticalLayout: this strategy put the elements the one below each other.
It is especially used to produce vertical menu bars.
corelib.services.web.javascript.jwt.BorderLayout: this strategy divides the container space into five areas. These five areas
are positioned like this: one to the north of the container, one to the west, one in the center, one in the east and finally one in the south. If peripheral areas will
not be used to contain a component, then the surface is reduced to zero pixel. Thus, if only one component is placed at the center of the
container, then it will take 100% of its area. It is especially used to place the tabs and the components that constitute a
TabbedPane component.
Attention!: this layout is almost the same in Java (AWT
and Swing).
Here is an example that shows the cutting of the five areas mentioned above.
To better understand the concept of layout, i propose you to study a small example which allows to
change the layout used by a container. To select the strategy to apply, we will use a JWT menu bar.
First, here is the code of the webpage: it especially defines a tag div that will be used to place the JWT container necessary for our
Demonstration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE html> <html> <head> <title>Layout Manager Sample</title> <meta content="IE=8" http-equiv="X-UA-Compatible"></meta> <script language="Javascript" src="corelib/services/web/javascript/Core.js"> </script> <script language="Javascript" src="LayoutManagers.js"> </script> <link rel="stylesheet" type="text/css" href="corelib/services/web/javascript/jwt/Jwt.css" /> </head> <body> <h1>Layout Manager Sample</h1> <br /> <center> <div id="forMenuBar"></div> <div id="forPanel"></div> </center> </body> </html> |
Then we must generate our container and the components that constitute it. Note that this example uses several types of JWT components and
even shows you how to define a new class of buttons. During the injection of the components into the container, through the method add,
a second parameter is used: it is the positioning imposition. In this example, the impositions are used only with the layout
of type BorderLayout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
importPackage( "corelib/services/web/javascript/jwt/FlowLayout.js" ); importPackage( "corelib/services/web/javascript/jwt/VerticalLayout.js" ); importPackage( "corelib/services/web/javascript/jwt/BorderLayout.js" ); importPackage( "corelib/services/web/javascript/jwt/Panel.js" ); importPackage( "corelib/services/web/javascript/jwt/DatePicker.js" ); importPackage( "corelib/services/web/javascript/jwt/ProgressBar.js" ); var MyButton = Component.extendClass( { initialize : function( text ) { this._htmlTagName = "input"; Component.prototype.initialize.call( this ); this.htmlTag.value = text; this.htmlTag.type = "button"; } } ); var panel = null; addEventHandler( window, "onload", function() { panel = new Panel( new BorderLayout() ); // \/ Only for BorderLayout panel.add( new DatePicker(), BorderLayout.NORTH ); panel.add( new MyButton( "Push me !" ), BorderLayout.WEST ); panel.add( new MyButton( "Pince me !" ), BorderLayout.CENTER ); panel.add( new MyButton( "Bidule me !" ), BorderLayout.EAST ); panel.add( new ProgressBar( 75 ), BorderLayout.SOUTH ); panel.setCssStyle( "width:500px; height: 120px;" ); panel.injectInDocument( document.getElementById( "forPanel" ) ); } ); |
Finally, we must manage the change of layout: to do so, we will define a menu bar with the three strategies proposed earlier. The click on any items of the menu will change the strategy used. Here is the final Javscript code to deliver. You will find immediately below the extract of code and the result produced by this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
importPackage( "corelib/services/web/javascript/jwt/FlowLayout.js" ); importPackage( "corelib/services/web/javascript/jwt/VerticalLayout.js" ); importPackage( "corelib/services/web/javascript/jwt/BorderLayout.js" ); importPackage( "corelib/services/web/javascript/jwt/Panel.js" ); importPackage( "corelib/services/web/javascript/jwt/DatePicker.js" ); importPackage( "corelib/services/web/javascript/jwt/MenuBar.js" ); importPackage( "corelib/services/web/javascript/jwt/Menu.js" ); importPackage( "corelib/services/web/javascript/jwt/MenuItem.js" ); var MyButton = Component.extendClass( { initialize : function( text ) { this._htmlTagName = "input"; Component.prototype.initialize.call( this ); this.htmlTag.value = text; this.htmlTag.type = "button"; } } ); var menuBar = null, panel = null; addEventHandler( window, "onload", function() { panel = new Panel( new BorderLayout() ); // \/ Only for BorderLayout panel.add( new DatePicker(), BorderLayout.NORTH ); panel.add( new MyButton( "Push me !" ), BorderLayout.WEST ); panel.add( new MyButton( "Pince me !" ), BorderLayout.CENTER ); panel.add( new MyButton( "Bidule me !" ), BorderLayout.EAST ); panel.add( new DatePicker( 75 ), BorderLayout.SOUTH ); panel.setCssStyle( "width:500px; height: 120px;" ); panel.injectInDocument( document.getElementById( "forPanel" ) ); menuBar = new MenuBar(); var layoutMenu = new Menu( "Layout Managers" ); var flowLayoutMenuItem = new MenuItem( "FlowLayout", "javascript: panel.setLayout( new FlowLayout( FlowLayout.CENTER ) );" ); layoutMenu.add( flowLayoutMenuItem ); var verticalLayoutMenuItem = new MenuItem( "VerticalLayout", "javascript: panel.setLayout( new VerticalLayout() );" ); layoutMenu.add( verticalLayoutMenuItem ); var borderLayoutMenuItem = new MenuItem( "BorderLayout", "javascript: panel.setLayout( new BorderLayout() );" ); layoutMenu.add( borderLayoutMenuItem ); menuBar.add( layoutMenu ); menuBar.setCssStyle( "width:500px; height: 24px;" ); menuBar.injectInDocument( document.getElementById( "forMenuBar" ) ); } ); |
|
|
|||||||