亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

FXML生成的JavaFX更改節點到另一個節點

FXML生成的JavaFX更改節點到另一個節點

墨色風雨 2022-10-07 16:44:14
我有一個由 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 文件中,但是當我運行一個測試項目時,一切正常。


查看完整回答
反對 回復 2022-10-07
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號