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!
for each
you need a
I prefer writing it in one line, because it's easier to read when there are many
without initializing
The
@FXML
annotation enables anFXMLLoader
to inject values defined in an FXML file into references in the controller class. In other words, if you annotate yourtimerLabel
with@FXML
, then it will be initialized by theFXMLLoader
when theload()
method is called by an element in the FXML file withfx:id="timerLabel"
. As others have pointed out in the comments, this means you should never write code likeHere
timerLabel
will first be initialized to thenew 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 thefx:id
, your variable will be referring to the wrongLabel
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 anfx:id
attribute into the controller. It will look forpublic
field with a variable name matching thefx:id
attribute, orpublic
or not) with a variable name matching thefx: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
etc will work, but
will not.