Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application Build a Pomodoro App Build the State Change Events

What is @FXML in the code?

Hey,

Sorry, although I saw this on previous videos, I am not clear about what exactly this means? I googled it and came across annotations. I could be wrong. Can some one give another example of this?

Thanks

1 Answer

Jura Streaming
Jura Streaming
8,082 Points

@whatever_stuff is annotation indeed, you are right. It is to put simply just metadata. Used for compiler and runtime.

Is is like @Override before method definition says to compiler "Hey, take a look at this, I want to override parent class method by this method. (toString() for example). The value in this is, that compiler will give you a thumbs down if no such method exists in parent. It helps to ensure things fit together as you wanted.

Now the @FXML annotation is same thing, except this time it is for JavaFX, at first compiler does not know anything about this annotation, hence it will be imported from 'import javafx.fxml.FXML;'. And it is to make sure things are connected to stuff in fxml files.

@FXML
private VBox container;

matches up with 'fx:id="container"

<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.SVGPath?>
<VBox stylesheets="/css/main.css"
      xmlns="http://javafx.com/javafx/8"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.teamtreehouse.pomodoro.controllers.Home"
      fx:id="container">
    <children>

Btw, the IntelliJ (perhaps other IDE's too) will even put a link near it to jump to fxml file where fx:id="container" resides.

You can create your own annotations too: https://docs.oracle.com/javase/tutorial/java/annotations/