Java Reference
In-Depth Information
most of the Java GUI components are built using it. One of the primary concepts
behind JavaBeans is the concept of properties, or named attributes. JavaBean
components have a set of properties accessed through get and set methods called
getters and setters. Another core feature is events. Events provide a standardized
mechanism for a JavaBean to notify an interested object that its state has
changed.
Though there is no built-in link between JavaFX and JavaBean components, it is
easy to write some glue code to bridge these two. To demonstrate this, we will
implement a number spinner in JavaFX based on the Java Swing class
JSpinner
.
A number spinner lets the user select an integer by sequencing through the num-
bers one at a time in either direction, but using mouse clicks. This is shown in
Figure 12.1.
Figure 12.1
Swing Number Spinner
To implement the
SpinnerNumber
class, we must first create a custom Swing
component, by extending the
javafx.ext.swing.SwingComponent
class.
public class SwingNumberSpinner extends SwingComponent {
Next, we need to implement the
createJComponent()
method from
SwingCom-
ponent
. This is where we instantiate the
javax.swing.JSpinner
object. The
default constructor for
JSpinner
automatically installs an integer data model,
javax.swing.SpinnerNumberModel
,
to support the
JSpinner
.
protected override function createJComponent(): JComponent {
new JSpinner();
}
For this particular example, the properties that we are really interested in are
found in the
SpinnerNumberModel
. These are the maximum and minimum value
for the spinner, the step size that tells the spinner how much to step when the
user selects up or down, and of course the value of the number. In JavaFX, we
will create instance variables for each of these.
public var value:Integer;
public var minimum:Integer;
public var maximum:Integer;
public var stepSize:Integer;
Search WWH ::
Custom Search