Java Reference
In-Depth Information
Similar to the DatePicker class, a ColorPicker allows users to select a color from a palette. The following two
lines of code, taken from Listing 6-12, show all that is needed to create the color picker control that is shown in
Figure 6-10 .
ColorPicker colorPicker = new ColorPicker(Color.BLUEVIOLET);
colorPicker.setOnAction(e -> System.out.println("Selected color: " + colorPicker.getValue()));
Again, clicking on the control will open a wider control where the user can select a color. When the selection is
done, the lambda expression in the onAction event handler on the colorPicker is called, and the selected color is
printed to the standard console.
We will now move on toward controls that graphically represent numeric values in various ranges.
Creating a Slider
The Slider control represents a numeric value with its thumb, and enables the user to choose a numeric value by
dragging its thumb. In Step 28 of the exercise you interacted with the Slider , shown in Figure 6-10 , to control the
value of the ProgressIndicator directly above it. The following snippet from Listing 6-12 contains the code that
realizes the Slider -related portions of this behavior:
final Slider slider = new Slider(-1, model.maxRpm, 0);
slider.setPrefWidth(200);
slider.valueProperty().bindBidirectional(model.rpm);
The range of the Slider is set through its min and max properties, which in this case are -1 and the value of the
maxRpm instance variable located in the StarterAppModel class. The min and max properties and the initial value are
passed via the constructor.
Also, the value property of the Slider is bidirectionally bound to the rpm property in the model, which is used for
keeping the ProgressIndicator updated as you experienced in Step 26. The following code snippet from Listing 6-1
contains the relevant instance variables from our StarterAppModel class:
public double maxRpm = 8000.0;
public DoubleProperty rpm = new SimpleDoubleProperty(0);
Defining a ProgressIndicator
The ProgressIndicator control displays the progress of an operation, either expressed as percent complete or
indeterminate. The following snippet contains the code that creates the ProgressIndicator and keeps its progress
property updated from the relevant instance variables in the model.
final ProgressIndicator progressIndicator = new ProgressIndicator();
progressIndicator.setPrefWidth(200);
progressIndicator.progressProperty().bind(model.rpm.divide(model.maxRpm));
As a result of the bind shown in this snippet, when the rpm variable in the StarterAppModel class is negative,
the progress property of the ProgressIndicator becomes negative. This causes the ProgressIndicator to assume
the indeterminate appearance that you experienced in Step 28 of the exercise. Note that we're using the Fluent API
covered in Chapter 4 in the bind expression.
 
Search WWH ::




Custom Search