私は、が呼び出された後、関数呼び出しの直後にヌルに行くと、fx:id
のコード参照がヌルではないという私の問題の縮小例があります。そのような参照を得る正しい方法は何ですか?これはsample.fxml
javafxの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がスローされます。
コントローラとして 'Application'サブクラスを使用しないでください。コントローラの新しいクラスを定義します。 –