问题
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>
You can see I want to use ${ddd}
to bind the text of textarea to a custom property from my.AppController
.
The code of my.AppController
:
public class AppController {
@FXML
public StringProperty ddd = new SimpleStringProperty("dddddddddd");
}
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?
回答1:
As of 02.2015 and Java 8, this seems to be not supported. You cant use expression binding to controller in FXML, I guess you have to do it in controller initialize code.
Actually I'm very dissapointed, this makes JavaFX FXML much much weaker technology compared to WPF XAML.
回答2:
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"/>
Controller
public class AppController {
@FXML
private TextArea textArea;
...
public void initialize(URL location, Resources resources) {
textArea.textProperty().bind(customProperty);
//textArea.setText("dddddddddd");
}
...
}
来源:https://stackoverflow.com/questions/23578281/how-to-bind-the-text-of-a-textarea-in-fxml-to-a-custom-property-in-the-controlle