Search code examples
javaeclipsejavafxefxclipse

JavaFX Eclipse suggestion to import not present


I am just starting with JavaFX and for some reason, Eclipse does not suggest to import JavaFX stuff so I have to type it in manually. Example: javafx.scene.Group I typed in Group as a type and it suggested other things such as javax.swing.GroupLayout.Group image of suggestions

I'm not exactly sure, but I think maybe the fact that the JavaFX library is under Modulepath instead of Classpath has to do with it, but it only works if it is under Modulepath.

Help would be greatly appreciated, thanks!


Solution

  • I'm rewriting this answer, as @kleopatra pointed out it was inexact

    For suggestions to work, you need to have JavaFX on your project's build path.

    If you've created a plain Java project in Eclipse, with a module-info.java at it's source path root, then you need to add all the required jars to the module path:

    • JavaFX SDK should already be installed on your system, or download it from Gluon and unzip it
    • Right-click your Java project, got to Build Path -> Configure Build Path...
    • Go to Libraries tab, point to the Modulepath section
    • Click [Add External JARs...] button, pick all the jars from JavaFX SDK, hit [Apply and close] Screenshot

    Then suggestions should work as expected, and imports will be automatically inserted at the top of your java files, but they will appear in error the first time you import anything from a given library module, as this module must also be required in your module-info.java. You can do that automatically with a quick fix ("Add 'requires javafx.something' to module-info.java") when hovering the import.

    Now there's a more straightforward way to create a JavaFX modular project in Eclipse, using Maven:

    • Create a new Maven Project
    • Check and pass the first wizard screen
    • Select archetype org.openjfx:javafx-archetype-fxml
    • On the next screen, you can select the JavaFX version of your choice (currently 13 by default)

    After the wizard terminates, you've got a nice little project already setup with an application window and two FXML sample displays. You can start from there to build your own project.

    Maven archetype

    Maven JavaFX Version