Java Reference
In-Depth Information
public void setProdId(String prodId) {
this.prodId.set(prodId);
}
The validation requirement and the convenience functionality are in the
initialize()
method, which will
be called by the
FXMLLoader
when it has finished loading the FXML file. We hooked up
ChangeListener
s to the
textProperty
of the two
TextField
s, and allow only valid changes to occur. We also move the cursor to
prodCode
when the
prefix
is filled with the correct data. Likewise, when we back off from the
prodCode
field, the cursor will
naturally jump to the
prefix
text field.
@FXML
public void initialize() {
prefix.textProperty().addListener((observable, oldValue, newValue) -> {
switch (newValue) {
case "A":
case "B":
case "C":
prodCode.requestFocus();
break;
default:
prefix.setText("");
}
});
prodCode.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.length() > 6) {
prodCode.setText(newValue.substring(0, 6));
} else if (newValue.length() == 0) {
prefix.requestFocus();
}
});
prodId.bind(prefix.textProperty().concat("-").concat(prodCode.textProperty()));
}
When the CustomComponent program is run, the Custom Component Example window shown in Figure
3-8
is displayed.
Figure 3-8.
The CustomComponent program
Search WWH ::
Custom Search