2017-01-16 23 views
0

私の.fxmlファイルのfirstNameとlastNameボックスから自分のコントローラにテキストを取得しようとしています。fxmlからテキストを取得する方法TextField

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Text?> 

<AnchorPane id="AnchorPane" fx:id="createPersonAnchor" prefHeight="247.0" prefWidth="285.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Button layoutX="171.0" layoutY="180.0" mnemonicParsing="false" onAction="#cancelCreatePersonAction" prefHeight="37.0" prefWidth="79.0" text="Cancel" /> 
     <TextField id="FirstName" fx:id="firstName" layoutX="115.0" layoutY="28.0" /> 
     <TextField id="LastName" fx:id="lastName" layoutX="115.0" layoutY="78.0" /> 
     <Button layoutX="47.0" layoutY="180.0" mnemonicParsing="false" onAction="#createPersonAction" prefHeight="37.0" prefWidth="79.0" text="Create" /> 
     <Text layoutX="29.0" layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="First Name:" wrappingWidth="69.677734375" /> 
     <Text layoutX="29.0" layoutY="95.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Last Name:" wrappingWidth="69.677734375" /> 
    </children> 
</AnchorPane> 

がコントローラ:

私は私がその後、人物オブジェクトに

FXMLを作成するためにそれらを使用することができますので、それらの姓と名のテキストフィールドには、コントローラに至るまでのテキストを渡すために取得する方法を見つけ出すことはできません

package project1; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.layout.AnchorPane; 

/** 
* FXML Controller class 
* 
* @author Fomnus 
*/ 
public class FXMLCreatePersonController implements Initializable { 

    /** 
    * Initializes the controller class. 
    */ 
    public FXMLCreatePersonController(){ 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLCreatePerson.fxml")); 
     loader.setController(this); 
     try{ 
      loader.load(); 
     }catch(Exception e){ 
      System.out.println(e.getMessage()+ "FXMLCreatePersonController failed"); 
     } 
    } 
    @FXML AnchorPane createPersonAnchor; 
    @FXML public void setFXMLCreatePersonAnchorPane(AnchorPane fxmlCreatePerson){ 
     createPersonAnchor = fxmlCreatePerson; 
    } 
    @FXML public AnchorPane getFXMLCreatePersonAnchorPane(){ 
     return createPersonAnchor; 
    } 
    @FXML 
    private void createPersonAction(ActionEvent event) { 

    } 
    @FXML 
    private void cancelCreatePersonAction(ActionEvent event) { 
     System.out.println("Hello World"); 
    } 
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    }  

} 

答えて

0

まず、あなたのFXMLにコントローラを設定します。

fx:controller=<Controller.Location> 
//Example : 
@FXML TextField textfield; 

そして最後に、あなたがする各ノードにIDを設定:は、あなたのcotrollerにあなたは初期化時にFXML.fileでそのIDにNodeに初期化される次の注釈@FXML参照元を使用しますコントロール(あなたがNodeに名前にゲームを参照のうえ:

fx:id=<idFromController> 

とテキストを取得するには:「firstNameの」および「lastNaの両方のIDセット:

textfield.getText(); 
+0

をはい私はFXを持っています私は私のcreatePersonAction関数のフィールドのwhatsにアクセスする方法を理解できません – Fomnus

+0

はい、しかし、あなたは主なものを忘れてしまった**コントローラ**、fx:controller =

関連する問題