0
읽은 후 introduction_to_fxml을 읽은 후 initialize 메서드를 spring의 afterPropertiesSet 또는 EJB의 @PostConstruct 메서드로 사용할 수 있다는 인상을 받았습니다. 즉, 호출 될 때 모든 멤버 변수가 설정 될 것으로 예상됩니다. 그러나 내가 시도했을 때 나는 NPE를 얻었다. 시도한 코드는 다음과 같습니다.NPE in initialize
메인 응용 프로그램 :
이public class MyApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/myapp.fxml"));///MAIN LOAD
Scene scene = new Scene(root, 320, 240);
scene.getStylesheets().add("/myapp.css");
stage.setScene(scene);
stage.setTitle("my app");
stage.show();
}
public static void main(String[] args) { launch(); }
}
myapp.fxml :
이...
<VBox fx:id="root" xmlns:fx="http://javafx.com/fxml" >
<ControlA>
<SomeClass>
</SomeClass>
</ControlA>
</VBox>
ControlA.java가 : 그 속성은 구성 요소의 초기화 방법의 load 메소드에서 호출된다
@DefaultProperty("aproperty")
public class ControlA extends StackPane {
private SomeClass aproperty;
public ContentPane(){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/controls/ControlA.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
fxmlLoader.load();//ControlA LOAD
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public void initialize() {
//aproperty is null here, called from ControlA LOAD
}
//aproperty get/set
public void setAproperty(SomeClass p){//it is called from MAIN LOAD
....
}
입니다
나중에 호출되는 부모의로드 메소드에서 설정됩니다. 이해할 수있는 것처럼 보입니다. 부모 fxml을 읽을 때까지 구성 요소의 속성 값을 구성 할 수 없습니다. 그렇다면 사용하기 전에 구성 요소를 초기화하고 모든 소품을 초기화 한 후에 모범 사례를 만드는 것이 가장 좋습니다.감사합니다.