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
import
statement for theStage
andScene
classes. - Add the
Stage
object literal. - Specify the title of the window.
- Add the
Scene
object literal to thescene
instance variable of theStage
class. - 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
import
statements for theToggleGroup
andRadioButton
classes. - Define the toggle group by using the
ToggleGroup
class.
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
choiceText
sequence 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
RadioButton
class. - Add each radio button to the group by using the
toggleGroup
instance 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
import
statements for theCircle
,Color
,RadialGradient
, andStop
classes. - Define the
colors
sequence 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 theselected
instance variable ofchoices[1]
istrue
, thecolor
instance 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 VBox
To lay out the circles horizontally:
- Add an
import
statement for theHBox
class. - Arrange the circles within the horizontal box.
- Specify the
spacing
variable to set an offset between the circles. - Set the
translateY
variable to25
so that the horizontal box can be located drawn down.
HBox{ spacing: 15 content: lights translateY: 25}
To arrange the radio buttons:
- Add an
import
statement for theVBox
class. - Use the cycle construction to add radio buttons to the container's content.
- Set the vertical offset between the radio buttons by using the
spacing
variable.
VBox{
spacing: 10
content: [for (i in [0..2]) choices[i] ]
}
- Use another
HBox
container to lay out the vertical box with the radio buttons and the horizontal box with the circles.
Note: All theContainer
classes constrain their children classes to fit a particular size, unlike theGroup
class that keeps its children size intact. Hence, if you would like to change a size of theRadioButton
added 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 theLayoutInfo
class 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
Tile
class. - Add the
Tile
object 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
vgap
variable.
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