2016-04-16 9 views
0

私はこのjavaFXアプリケーションを作っていますが、与えられた瞬間に、シーン内のテキストをanotheクラスのメソッドから取得したいのですが、この値をテキストボックスにどのように渡すことができますかFXML文書の中で?fxmlシーンに値を渡す方法

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.effect.*?> 
<?import javafx.scene.image.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 
<?import java.lang.*?> 
<?import javafx.scene.control.*?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.72" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.crapgames.calourosimulator.views.pcna.intro.Introduction"> 
    <children> 
     <ImageView fx:id="imgClick" fitHeight="600.0" fitWidth="800.0" onMouseClicked="#nextScene" pickOnBounds="true"> 
     <image> 
      <Image url="@../../assets/pnca-monitor-text.png" /> 
     </image> 
     </ImageView> 
     <Text fx:id="text1" fill="#f5eded" layoutX="8.0" layoutY="452.0" strokeType="OUTSIDE" strokeWidth="0.0" text="where i want my string variable" wrappingWidth="787.0"> 
     <font> 
      <Font name="Comic Sans MS" size="25.0" /> 
     </font></Text> 
    </children> 
</AnchorPane> 

答えて

0

テキスト値を受け入れるようにコントローラクラス内にメソッドを作成します。あなたがしたい場合は、TexttextPropertyを活用することができます

public class Introduction { 

    @FXML 
    private Text text1 ; 

    public StringProperty textProperty() { 
     return text1.textProperty(); 
    } 

    public final void setText(String text) { 
     textProperty().set(text); 
    } 

    public final String getText() { 
     return textProperty().get(); 
    } 

    // existing code ... 

} 

は今、あなたは行うことができます。

FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml/file.fxml")); 
Parent root = loader.load(); 
Introduction controller = loader.getController(); 
controller.setText("Hello World!"); 

Scene scene = new Scene(root); 

// etc... 
関連する問題