多个StringProperty的Javafx连接
有没有一种简单的方法可以绑定StringProperty对象的串联 以下是我想做的:多个StringProperty的Javafx连接,string,javafx,concatenation,observable,String,Javafx,Concatenation,Observable,有没有一种简单的方法可以绑定StringProperty对象的串联 以下是我想做的: TextField t1 = new TextField(); TextField t2 = new TextField(); StringProperty s1 = new SimpleStringProperty(); Stringproperty s2 = new SimpleStringProperty(); Stringproperty s3 = new SimpleStringProperty()
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() );
textfieldt1=newtextfield();
TextField t2=新的TextField();
StringProperty s1=新的SimpleStringProperty();
Stringproperty s2=新的SimpleStringProperty();
Stringproperty s3=新的SimpleStringProperty();
s1.bind(t1.textProperty());//绑定t1的文本
s2.bind(t2.textProperty());//绑定t2的文本
//理论上,我想做的是:
s3.bind(s1.getValue()+“”+s2.getValue());
我该怎么做?您可以:
s3.bind(Bindings.concat(s1, " <some Text> ", s2));
好吧,它似乎不起作用。如果我绑定标签的StringProperty,它会显示:
StringProperty[bound,invalid]StringProperty[bound,invalid]
更改s1和s2的值不会更改任何内容。添加了完整示例。(我使用的是JDK1.8.0_11,fwiw)您是否在concat(…)
调用中使用了+
,在调用中使用了。。(掩饰我的羞耻)。非常感谢,问题解决了:)@BradTurek在功能上没有,但是Bindings.concat(…)
是一个varargs方法,所以在这里更方便(您需要tf1.textProperty().concat(“:”).concat(tf2.textProperty())
)。
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);
}
}