我有一个由 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);
1 回答
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
您可以fx:factory
按照FXML 简介中的说明使用。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import org.controlsfx.control.textfield.TextFields?>
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.example.Controller">
<TextFields fx:id="field" fx:factory="createClearableTextField"/>
</StackPane>
然后在控制器中使用它:
package com.example;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class Controller {
@FXML private TextField field;
}
注意:如果您使用的是 IntelliJ,它会发出一条错误消息:
无法将 org.controlsfx.control.textfield.TextFields 设置为字段“字段”
在 FXML 文件中,但是当我运行一个测试项目时,一切正常。
添加回答
举报
0/150
提交
取消