如何将fxml中textarea的文本绑定到控制器中的自定义属性? [英] How to bind the text of a textarea in fxml to a custom property in the controller?

查看:666
本文介绍了如何将fxml中textarea的文本绑定到控制器中的自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个fxml文件:

I have a fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
      prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
      fx:controller="my.AppController">
    <children>
        <TextArea prefHeight="200.0" prefWidth="200.0" text="${ddd}"/>
    </children>
</HBox>

你可以看到我想要使用 $ {ddd} 将textarea的文本绑定到来自 my.AppController 的自定义属性。

You can see I want to use ${ddd} to bind the text of textarea to a custom property from my.AppController.

代码 my.AppController

public class AppController {

  @FXML
  public StringProperty ddd = new SimpleStringProperty("dddddddddd");
}

当我运行这个javafx应用程序时,它在textarea中没有显示任何内容,似乎无法从 AppController 绑定到 ddd

When I run this javafx application, it doesn't show anything in the textarea, seems it can't bind to the ddd from AppController.

这样做的正确方法是什么?

What's the correct way to do it?

推荐答案

制作 TextArea text绑定到属性,使用 fx:id 。将 fx:id 分配给 TextArea ,在控制器中使用它并绑定其<$​​ c $ c> textProperty ()到您喜欢的任何房产。您甚至可以直接声明文本(如果您正在寻找它)。

To make the TextArea text bind to a property, use fx:id. Assign a fx:id to the TextArea, use it in the controller and bind its textProperty() to any property you like. You can even directly declare the text (if you are looking for it).

<TextArea fx:id="textArea" prefHeight="200.0" prefWidth="200.0"/>

控制器

public class AppController {

  @FXML
  private TextArea textArea;

  ...

  public void initialize(URL location, Resources resources) {
       textArea.textProperty().bind(customProperty);
       //textArea.setText("dddddddddd");
  }

  ...

}

这篇关于如何将fxml中textarea的文本绑定到控制器中的自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆