string - 多个 StringProperty 的 Javafx 串联

标签 string javafx concatenation observable

有没有一种简单的方法来绑定(bind) StringProperty 对象的串联?

这是我想做的事情:

TextField t1 = new TextField();
TextField t2 = new TextField();

StringProperty s1 = new SimpleStringProperty();
Stringproperty s2 = new SimpleStringProperty();
Stringproperty s3 = new SimpleStringProperty();

s1.bind( t1.textProperty() ); // Binds the text of t1
s2.bind( t2.textProperty() ); // Binds the text of t2

// What I want to do, theoretically :
s3.bind( s1.getValue() + " <some Text> " + s2.getValue() );

我怎样才能做到这一点?

最佳答案

你可以做:

s3.bind(Bindings.concat(s1, "  <some Text>  ", s2));

这是一个完整的例子:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class BindingsConcatTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        Label label = new Label();

        label.textProperty().bind(Bindings.concat(tf1.textProperty(), " : ", tf2.textProperty()));

        VBox root = new VBox(5, tf1, tf2, label);
        Scene scene = new Scene(root, 250, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

关于string - 多个 StringProperty 的 Javafx 串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325209/

相关文章:

java - Oracle 看到的字符串的真实长度

python - Python 中的子字符串。内存中的副本?

css - Javafx 中没有颜色方 block 的 ColorPicker?

java - 字符串数组串联的问题

concatenation - 如何在 TensorFlow 中对批处理进行切片并对每个切片应用操作

swift - SubSequence 索引的兼容性

java - 我需要从 java 中的属性文件中将变量作为字符串传递

svg - JavaFX 如何沿着给定的 SVG 路径拖动对象 - 就像一个 slider

java - 将类对象发送到阶段 Controller - javafx

SQL Server 2008 行到 1 个 CSV 字段