Java Reference
In-Depth Information
When the variable
panel
is added to a
Scene
instance (not shown) and the application is
executed, you will end up with what is shown in the next screenshot:
How it works...
The previous code shows how to use the Slider control to create a color picker. It declares
three
Slider
instances to represent a color element including
rSlide
for red,
gSlide
for
green, and
bSlide
for blue. The most pertinent properties of the Slider are
min:Number
and
max:Number
. These properties specify the range of values that the slider represents. In
our code, each slider instance is declared with
min=0
and
max=255,
corresponding to the
numeric ranges of the RGB color values. The user can only slide the knob between the
min
value and the
max
values (inclusive).
As the user drags the knob, the slider updates its
value:Number
property. This property
indicates the currently selected value based on the knob position on the track. For instance,
when the user slides the knob all the way to the right-hand side, the
value
property of the
slider will be set to
255
.
The Circle instance
circ
is used to reflect the color changes as the slider values are updated.
To do this, the
fill:Color
property of the circle is assigned a
Color
instance, where each
element of the RGB color is bound to slider
rSlide.value
,
gSlide.value
, and
bSlide.
value
respectively, as shown in the next code snippet. Whenever a color value changes, the
bind generates a new
Color
instance to apply to the circle.
var circ = Circle {
fill :
bind
Color.rgb(
rSlide.value, gSlide.value, bSlide.value
)
...
}
See also
F
Introduction
F
Creating a form with JavaFX controls