DEV Community

Cover image for Hide JavaFX FXML Warnings in IntelliJ IDEA
apwhitelaw
apwhitelaw

Posted on

Hide JavaFX FXML Warnings in IntelliJ IDEA

If you're familiar with JavaFX, you're probably familiar with the concept of linking your UI element to your controller by setting the fx:id property on your elements and creating a matching field in your controller. This feature makes the process of accessing your new UI element a breeze. But unfortunately when we use @FXML our IDE isn't able to see that these fields and methods are being used. As far as the IDE is aware, the fields are unassigned, and the methods are unused.

IntelliJ Inspections - Errors, Warnings, and more

Before explaining how to hide these errors, it is important to understand the Inspections tool.

Inspections is an automated code analysis tool that checks your code automatically and can identify a wide range of issues such as syntax errors, code redundancies, performance bottlenecks, and security vulnerabilities. By running inspections on your code, you can catch potential problems early on and improve the overall quality of your code. IntelliJ IDEA provides a wide range of built-in inspections that are run automatically against your code and produce the errors, warnings, and suggestions that are used to seeing in IntelliJ.

You can customize the inspections to your need, as well as create brand new ones. Don't like it warning you that your catch block is empty? Turn off that inspection entirely! Want to check for something not built-in? Create the inspection yourself! IntelliJ creates a new inspections profile for each project, which is great because you may only want to make changes for the current project. You can also save an inspections profile and reuse it later.

The Solution

At the end of the day we can simply ignore the FXML fields/methods warnings and our project will run perfectly fine (assuming everything is properly linked). But if you're like me, you'd like to be able to clear out all the warnings. Mainly because when dealing with a large number of UI elements, the warnings tend to accumulate rapidly. It feels so wrong to see 40 warnings and go "Yep, that's correct."

With some inspections, it gives you the option to automatically ignore this warning, or ignore it just in this one instance. For our case, there is no option to automatically hide or remove the warning. A quick way you could hide these warnings through code is by using @SuppressWarnings("unused"). Placing this annotation above a field or method will hide the unused warning attached to it, but adding this tag to each field and method is a bit tedious. Instead, you can simply add the annotation to the whole class by putting the annotation on the line above the start of your public class ... line. Still, this will require you to add the annotation to each controller class.

For a much more robust solution, we can simply setup the inspections to consider these fields as assigned and methods as being used. How do we do that? (Follow red boxes in below picture) Well, first we must go to settings and open Editor>Inspections. Inside there search for Unused declaration then click Java>Unused declaration in the results. On the right, click Entry points, then click Annotations.

Settings Window

Once you've clicked Annotations you should see the following window.

Annotations Window

Here there are two boxes, and by default they should be blank. The first one, mark as entry point will tell the IDE that a method has an entry point, meaning it is being used somewhere and therefore is not "unused".

The other box `mark field as implicitly written' will tell the IDE that the field is assigned a value even though it is not explicitly written. We can't read through our code and find the line where a particular field is assigned, but we know the fields are being assigned and don't need to have a warning.

Simply click the plus button for each box and add the FXML annotation. Once you apply that, both sets of warnings should be gone for any methods annotated with @FXML!

Top comments (0)