Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
820 views
in Technique[技术] by (71.8m points)

javafx 8 - Static @FXML variables in FXML Controller

I upgraded from JavaFX 8 b109 to b116 and my FXML controller class has the injected variables as null when it initializes. I tried it with the sample NetBeans FXML app.

@FXML
private static Label label;

@Override
public void initialize(URL url, ResourceBundle rb) {
    label.setText("static test");
} 

The problem is with the static keyword. I remove that and it works. It was working fine in b109 with static variables. When writing my app initially, I figured static made sense. They're created when the application loads and there's only ever one copy of the scene.

Is there a way around this without rewriting a thousand lines of code that use static access?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thank you very much for documenting this issue. Since last night, I try to fix it. Like you I guessed that the injected variables are null and could at finally shape my question to query the web for a solution. I found your entry here.

For this problem I suggest a quick dirty solution, I removed the static keyword from the declaration of original variable name at the same time I declared a static equivalent of it inserting a _ character at the end of the original name. In initialize() I've copied the injected objects into the static counterparts without having to refactor all my static inner classes and methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...