2016-10-06 6 views
0

widthProperty()のデータを正しく使用する方法textFieldtextField、JavaFxのデータで矩形を描画する

widthRectangle.textProperty().bindBidirectional(rectangleObj.getWidthRectangleValue()); 
rectangle.widthProperty().bind(rectangleObj.getWidthRectangleValue()); 

上記は機能しません。たぶん私はそれを適切に使う方法をどこかに見つけるでしょう?

編集:

public class RectangleObj { 


    private StringProperty widthRectangleValue = new SimpleStringProperty(); 
    private StringProperty heightRectangleValue = new SimpleStringProperty(); 

    public StringProperty getWidthRectangleValue(){ 
     return widthRectangleValue; 
    } 

    public void setWidthRectangleValue(StringProperty widthRectangleValue){ 
     this.widthRectangleValue = widthRectangleValue; 
    } 


    public StringProperty getHeightRectangleValue(){ 
     return heightRectangleValue; 
    } 

    public void setHeightRectangleValue(StringProperty heightRectangleValue){ 
     this.heightRectangleValue = heightRectangleValue; 
    } 
} 

public class ControllerParametersForRectangle implements Initializable { 


    @FXML 
    Rectangle rectangle; 

    @FXML 
    TextField widthRectangle; 

    @FXML 
    TextField heightRectangle; 

    RectangleObj rectangleObj = new RectangleObj(); 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

     widthRectangle.textProperty().bindBidirectional(rectangleObj.getWidthRectangleValue()); 
     rectangle.widthProperty().bind(rectangleObj.getWidthRectangleValue()); 


    } 
} 
+0

あなたの質問は正確には何ですか? –

+0

私は 'bind()'の代わりに何を作るべきかわかりません – Bartek

+1

"うまくいかない"とはどういう意味ですか? 'rectangleObj'が何であるのか、' getWidthRectangleValue() 'が返すものをテレパシーで知っていると思われますか? [質問する](http://stackoverflow.com/help/how-to-ask)を読んでください。 –

答えて

3

StringPropertyに値をバインドしないでください。 TextFromatterを使用してTextFieldのテキストを別のタイプに変換してください。

例:

@Override 
public void start(Stage primaryStage) { 
    Rectangle rect = new Rectangle(100, 100); 

    StringConverter<Double> converter = new DoubleStringConverter(); 

    TextField xTextField = new TextField(); 
    TextFormatter<Double> xFromatter = new TextFormatter<>(converter); 
    xTextField.setTextFormatter(xFromatter); 

    TextField widthTextField = new TextField(); 

    TextFormatter<Double> widthFromatter = new TextFormatter<Double>(converter); 
    widthTextField.setTextFormatter(widthFromatter); 

    xFromatter.valueProperty().bindBidirectional(rect.xProperty().asObject()); 
    widthFromatter.valueProperty().bindBidirectional(rect.widthProperty().asObject()); 

    Scene scene = new Scene(new VBox(10, xTextField, widthTextField, new Pane(rect)), 500, 500); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

よかった、ありがとう。これが私の主張です – Bartek