Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javafx - Is @FXML needed for every declaration?

Is @FXML needed for every declaration or just for the first?

In other words, should I use

@FXML
public Label timerLabel = new Label();
@FXML
public TextField mainTextField, projectTextField ;
@FXML
public Button goButton, deleteAllButton ;
@FXML
public ComboBox<String> projectComboBox ;
@FXML
public TableView<Entry> mainTable ;
@FXML
public TableColumn<Entry, String> titleColumn, timeColumn, dateColumn ;
@FXML
public TableColumn<Entry, Boolean> checkColumn, buttonColumn ;
@FXML
public checkBox checkAllCheckBox ;

Or

@FXML
public Label timerLabel = new Label();
public TextField mainTextField, projectTextField ;
public Button goButton, deleteAllButton ;
public ComboBox<String> projectComboBox ;
public TableView<Entry> mainTable ;
public TableColumn<Entry, String> titleColumn, timeColumn, dateColumn ;
public TableColumn<Entry, Boolean> checkColumn, buttonColumn ;
public checkBox checkAllCheckBox ;

Thank you!

Question&Answers:os

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

1 Answer

0 votes
by (71.8m points)

The @FXML annotation enables an FXMLLoader to inject values defined in an FXML file into references in the controller class. In other words, if you annotate your timerLabel with @FXML, then it will be initialized by the FXMLLoader when the load() method is called by an element in the FXML file with fx:id="timerLabel". As others have pointed out in the comments, this means you should never write code like

@FXML
private Label timerLabel = new Label();

Here timerLabel will first be initialized to the new Label(); you create in the code, and will then almost immediately be re-initialized to the value defined in the FXML file. This is at best redundant, and at worst misleading. If you don't correctly match the variable names to the fx:id, your variable will be referring to the wrong Label and the error will be very difficult to track down.

To get to your actual question:

When the FXMLLoader loads the FXML file, it will attempt to inject any elements that have an fx:id attribute into the controller. It will look for

  1. Any public field with a variable name matching the fx:id attribute, or
  2. Any field (public or not) with a variable name matching the fx:id attribute that is annotated with @FXML.

So in your example, since all your fields are public, you can omit all the @FXML annotations (even the first) and it will still work.

However, if you follow good practice and make your fields private, then each declaration must be annotated @FXML for the injection to work.

So

@FXML
private Label timerLabel;
@FXML
private TextField mainTextField;

etc will work, but

@FXML
private Label timerLabel;
private TextField mainTextField;

will not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...