2016-11-25 3 views
1

私は、が呼び出された後、関数呼び出しの直後にヌルに行くと、fx:idのコード参照がヌルではないという私の問題の縮小例があります。そのような参照を得る正しい方法は何ですか?これはsample.fxmljavafxのfx:idで記述されたノードへの参照を取得するには?

<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sample.Main"> 
    <columnConstraints> 
     <ColumnConstraints /> 
    </columnConstraints> 
    <rowConstraints> 
     <RowConstraints /> 
    </rowConstraints> 
    <children> 
     <Text fx:id="textRef" strokeType="OUTSIDE" strokeWidth="0.0" text="Hello world" /> 
    </children> 
</GridPane> 

であり、これはそのコントローラーとして宣言されてMain.javaです。

public class Main extends Application implements Initializable{ 
    @FXML 
    public Text textRef; 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 300, 275)); 
     primaryStage.show(); 

     this.someNewFunction(); 
    } 

    private void someNewFunction() { 
     this.textRef.setText("Changed in somNewFunction"); 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     this.textRef.setText("Changed at initialize"); 
    } 
} 

テキストrefはinitializeコール内部有効ですが、someNewFunction内部にNullPointerExceptionがスローされます。

+0

コントローラとして 'Application'サブクラスを使用しないでください。コントローラの新しいクラスを定義します。 –

答えて

3

Mainインスタンスをコントローラとして使用するFXMLLoaderによって作成Mainインスタンスとは異なる目的で役立つ

希望。

私見FXMLをロードした後FXMLLoaderからコントローラを取得し、また、コントローラとしてApplicationに異なるクラスを使用する方が良いでしょう:

public class MainController implements Initializable { 

    ... 

} 
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sample.MainController"> 
    ... 
</GridPane> 
@Override 
public void start(Stage primaryStage) throws Exception { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); 
    Parent root = loader.load(); 
    primaryStage.setTitle("Hello World"); 
    primaryStage.setScene(new Scene(root, 300, 275)); 
    primaryStage.show(); 

    MainController controller = loader.getController(); 

    controller.someNewFunction(); 
} 

ただし、fxmlで使用するコントローラを指定することもできます。

<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"> 
    ... 
</GridPane> 
@Override 
public void start(Stage primaryStage) throws Exception { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); 
    loader.setController(this); 
    Parent root = loader.load(); 
    primaryStage.setTitle("Hello World"); 
    primaryStage.setScene(new Scene(root, 300, 275)); 
    primaryStage.show(); 

    this.someNewFunction(); 
} 
-1

javafxループの外側でsomeNewFunctionを実行しています。 私は、showメソッドが終了した後、メインスレッドが制御を引き継ぎ、javafxコンポーネントはもはや存在しなくなりました(それらはもはや必要ではないと思われているので破棄されます)。 someNewFunctionをいくつかのFXML要素参照(つまり、SceneBuilderのsetOnAction経由またはコード経由)にバインドしたイベントにバインドするか、initializeメソッドの中にメソッド呼び出しを置くか、または単に呼び出す前にprimaryStage.show ()行。これが起動される

+0

javafx以外のイベントを処理するjavafxの方法は何ですか?たとえば、 'someNewFunction'の中にjsonファイルを解析しなければならない場合 – Subra

+1

この答えの文は真です。 –

関連する問題