我有一个由 fxml 文件定义的复杂 UI。有很多 stockTextField我想更改为ControlsFX库提供的更高级版本(可清除的文本字段)。
问题:
ControlsFX 用于CustomTextField提供高级功能,但它只能被初始化,TextFields.createClearableTextField()只能从代码中调用。
我正在寻找一种TextField将通过 FXMLCustomTextField初始化的 s 与从代码初始化的 s 交换的方法,以便这些新控件将保留在 FXML 中定义的所有布局属性。
CustomTextField在 FXML 中 定义也无济于事,因为默认情况下它是无用的。CustomTextField获得了private static void setupClearButtonField(TextField inputField, ObjectProperty<Node> rightProperty)我无法调用的功能,因为它是私有的。
ControlsFX 代码:
/**
* Creates a TextField that shows a clear button inside the TextField (on
* the right hand side of it) when text is entered by the user.
*/
public static TextField createClearableTextField() {
CustomTextField inputField = new CustomTextField();
setupClearButtonField(inputField, inputField.rightProperty());
return inputField;
}
/**
* Creates a PasswordField that shows a clear button inside the PasswordField
* (on the right hand side of it) when text is entered by the user.
*/
public static PasswordField createClearablePasswordField() {
CustomPasswordField inputField = new CustomPasswordField();
setupClearButtonField(inputField, inputField.rightProperty());
return inputField;
}
private static void setupClearButtonField(TextField inputField, ObjectProperty<Node> rightProperty) {
inputField.getStyleClass().add("clearable-field"); //$NON-NLS-1$
Region clearButton = new Region();
clearButton.getStyleClass().addAll("graphic"); //$NON-NLS-1$
StackPane clearButtonPane = new StackPane(clearButton);
clearButtonPane.getStyleClass().addAll("clear-button"); //$NON-NLS-1$
clearButtonPane.setOpacity(0.0);
clearButtonPane.setCursor(Cursor.DEFAULT);
clearButtonPane.setOnMouseReleased(e -> inputField.clear());
clearButtonPane.managedProperty().bind(inputField.editableProperty());
clearButtonPane.visibleProperty().bind(inputField.editableProperty());
rightProperty.set(clearButtonPane);
final FadeTransition fader = new FadeTransition(FADE_DURATION, clearButtonPane);
fader.setCycleCount(1);
小怪兽爱吃肉
相关分类