2016-04-06 9 views
0

フレームがあり、テキストフィールドを持つレイアウトはnaffですが、現時点では目的はありません。テキストフィールドのエントリに基づいて文字列のプロパティを更新します。

は、私は(.SET呼び出すことによって、コード自体の中に更新する

同様に(日食)テキストフィールドに入力されているものに基づいて、文字列プロパティを更新し、そしてそれはまた、以下のコンソールでプリントアウトしたいですここでは)

は私のコードです -

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Font; 

public class TextEntry extends Application 
{ 
    private static StringProperty text = 
      new SimpleStringProperty("text"); 

    public static void main(String [] args) 
    { 
     launch(args); 
    } 
    public void start(Stage primaryStage) 
    { 
     Pane root = new Pane(); 

     TextField enterText = new TextField(); 
     enterText.setFont(Font.font("SanSerif",20)); 

     enterText.setOnMousePressed(e ->{ 
      text.bind(enterText.textProperty()); 
      System.out.println("the new value is: " + text); 
     }); 

     root.getChildren().addAll(enterText); 
     Scene scene = new Scene(root,600,600); 
     primaryStage.setScene(scene); 
     primaryStage.setTitle("Text Entry"); 
     primaryStage.show(); 
    } 
} 

私は上記のコードの行を含むカップルの事を試してみました -

text.set("").bind(enterText.textProperty()); 
、任意のアイデア

text.textProperty().bind(enterText.textProperty()); 

の第二は、私が実現文法的に正しくないですが、私は解決策を考えることはできませんか?

答えて

0

プロパティを一度バインドするだけで、キーを押すたびにバインドする必要はありません。それからちょうどプロパティにChangeListenerを追加します。もちろん

public void start(Stage primaryStage) { 
    Pane root = new Pane(); 

    TextField enterText = new TextField(); 
    enterText.setFont(Font.font("SanSerif",20)); 

    text.bind(enterText.textProperty()); 

    text.addListener((obs, oldTextValue, newTextValue) -> 
     System.out.println("The new value is "+newTextValue)); 

    root.getChildren().addAll(enterText); 
    Scene scene = new Scene(root,600,600); 
    primaryStage.setScene(scene); 
    primaryStage.setTitle("Text Entry"); 
    primaryStage.show(); 
} 

を、余分な性質上記のコードでは、冗長である:あなただけ

enterText.textProperty().addListener((obs, oldTextValue, newTextValue) -> 
    System.out.println("The new value is "+newTextValue)); 

を行うことができますが、多分あなたは、追加を必要とする他の理由がありますプロパティ。

+0

すごい、ありがとう!それは私に少し迷惑をかけるようになっていた! – Treeno1

関連する問題