Lesson 6: Laying Out GUI Elements
Consider creating three circles and a toggle group of radio buttons. Each radio button controls a color in the corresponding circle. When the radio button is selected the color is applied, otherwise the circle is gray. This scenario simulates a traffic light and is the example used in this tutorial to describe the layout mechanism.
| Figure 1: Stop Light | Figure 2: Ready Light | Figure 3: Go Light | 
To create a window:
- Add an importstatement for theStageandSceneclasses.
- Add the Stageobject literal.
- Specify the title of the window.
- Add the Sceneobject literal to thesceneinstance variable of theStageclass.
- Set the width and the height of the scene.
| import javafx.stage.Stage; | 
Create a group of radio buttons in which only one button can be selected at a given time. This is called a toggle group. To define radio buttons:
- Add importstatements for theToggleGroupandRadioButtonclasses.
- Define the toggle group by using the ToggleGroupclass.
 Note: In the example code for this lesson, you define a variable name for each UI element so that you can easily learn how the layout mechanism works. You also apply binding to the UI elements by referring to the variable names.
- Define the choiceTextsequence for the button captions.
- Create a sequence of radio buttons within a cycle construction. The buttons enable selection of the particular traffic light by using the RadioButtonclass.
- Add each radio button to the group by using the toggleGroupinstance variable.
The following code fragment defines a toggle group, then creates radio buttons that add them to the group.
| import javafx.scene.control.ToggleGroup; | 
To draw circles that indicate the traffic lights:
- Add importstatements for theCircle,Color,RadialGradient, andStopclasses.
- Define the colorssequence for the colors of the circles.
- Create a sequence of circles within a cycle construction.
- Apply radial gradient filling to visually enhance the example.
 import javafx.scene.shape.Circle; 
 import javafx.scene.paint.Color;
 import javafx.scene.paint.RadialGradient;
 import javafx.scene.paint.Stop;
 var colors = ["RED", "GOLD", "GREEN"];
 var lights = for (color in colors)
 Circle {
 centerX: 12
 centerY: 12
 radius: 12
 stroke: Color.GRAY
 fill: bind RadialGradient {
 centerX: 8,
 centerY: 8,
 radius: 12,
 proportional: false
 stops: [
 Stop {offset: 0.0 color: Color.WHITE},
 Stop {offset: 1.0 color:
 if (choices[indexof color].selected)
 then Color.web(color)
 else Color.GRAY
 }//Stop
 ]
 }//RadialGradient
 }//Circle
 The preceding code sample uses the data binding mechanism to change the color of the circle. If theselectedinstance variable ofchoices[1]istrue, thecolorinstance variable becomesColor.RED, otherwise it isColor.GRAY. Refer to Creating Graphical Objects for more information about shapes and visual effects.
After all the components are created, you can use layout containers to arrange elements within the scene. This lesson explores the JavaFX Container: HBox, VBox, and Tile. In this example, you need a VBox object to lay out the radio buttons, an HBox object  for the circles, and another HBox to arrange those two boxes.
HBox and VBoxTo lay out the circles horizontally:
- Add an importstatement for theHBoxclass.
- Arrange the circles within the horizontal box.
- Specify the spacingvariable to set an offset between the circles.
- Set the translateYvariable to25so that the horizontal box can be located drawn down.
 HBox{ spacing: 15 content: lights translateY: 25}
 
To arrange the radio buttons:
- Add an importstatement for theVBoxclass.
- Use the cycle construction to add radio buttons to the container's content.
- Set the vertical offset between the radio buttons by using the spacingvariable.
 VBox{
 spacing: 10
 content: [for (i in [0..2]) choices[i] ]
 }
 
- Use another HBoxcontainer to lay out the vertical box with the radio buttons and the horizontal box with the circles.
Note: All theContainerclasses constrain their children classes to fit a particular size, unlike theGroupclass that keeps its children size intact. Hence, if you would like to change a size of theRadioButtonadded to theVBox, its size is not honored, because the container resizes the node to its preferred size. To change the predefined size of the UI control use theLayoutInfoclass as follows:
RadioButton{
toggleGroup: group
text: text
layoutInfo: LayoutInfo { width: 150 }
}//RadioButton
See the complete code of the example as follows.
| import javafx.stage.Stage; | 
When compiled and run, this code produces the following window.
Alternatively you can use another layout manager represented by the Tile class to arrange the same components, but in a slightly different order. The Tile layout arranges elements in a grid of cells. Each cell is exactly the same size. The rows and columns instance variables define the  grid's size. 
To apply the Tile layout:
- Add an import statement for the Tileclass.
- Add the Tileobject literal to the scene's content.
- Define the number of rows and columns in the grid.
- Use a cycle construction to arrange elements within the grid so that each row contains a radio button and a circle.
- Define the amount of vertical space between elements in a column by using the vgapvariable.
The following code fragment shows how to create a Tile object with three rows and two columns. 
| Tile { | 
See the complete code of the example as follows.
| import javafx.stage.Stage; | 
When compiled and run, the modified application produces the following window.
Try to lay out other components, for example, buttons.
0 comments:
Post a Comment