How to convert a normal java project in intellij into a JavaFx project
Asked Answered
M

5

20

I am currently using a normal java project (gradle project) and want to convert this into a JavaFx project without having to reimport the existing sources into a new Javafx project. Is there any way to achieve this?

Maldon answered 2/5, 2014 at 5:24 Comment(0)
M
19

There is nothing to convert.

I don't believe there is any difference in Idea between a "normal" Java project and a JavaFX project.

Sure, in Idea 12.x+ there is a wizard you can use to create a new JavaFX project. But I think that all it does is create a sample hello world application - after initial creation, the way the project works is not any different than any other Java project.

And this is how it should be, JavaFX is just Java. Oracle doesn't differentiate between JavaFX and Java in their distribution, and neither should IDEs in their project types.

I believe, in this case, even if I'm wrong, that I am right enough that it doesn't really matter if I'm wrong.

Update

So I was wrong enough that it matters :-)

Using Idea 13.1.4, if I create a new project using File | New Project | Java, there are the following resource settings (File | Settings | Compiler):

?*.properties;?*.xml;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.html;?*.dtd;?*.tld;?*.ftl

That is, the resources for the project are just set to copy only specific file types. So you could modify it to get the additional file types required in some JavaFX projects by adding resource copying support for fxml and css; i.e, appending ;?*.fxml;?*.css.

The interesting part is that if you create a new project using File | New Project | JavaFX, there are the following resource settings:

!?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj

Essentially, it's copying everything which not a source file, a kind of file blacklist set rather than a file whitelist set as is used by other project creation templates. Really weird... Anyway, the resource sets are user configurable, so you can modify them as you see fit and once you do so, you shouldn't have any issues (I believe, but I've been wrong before ;-)

Suggestion - Use 3rd party build tools in conjunction with your IDE

You might be better off basing your build off of a 3rd party tool such as Gradle or Maven. Idea works really well with both these external build tools (and others). The advantages of using a 3rd party tool is:

  1. Projects with those tools follow a convention where resources are placed in a specific resource folder and everything in that folder is treated as a resource to be packaged in your build output. So there is less confusion there.
  2. The resultant projects are more portable and easier to use by other developers who may not be using Idea.
  3. The projects can be built easily by continuous integration build systems like Jenkins.
  4. Both Gradle and Maven have JavaFX specific plugins which provide additional support for packaging JavaFX applications.

Of course, the disadvantage of using a 3rd party build tool, is the complexity (and many quirks) of learning them as sometimes they can be quite the beast. So I think it's a bit of a tradeoff - small, personal exploratory projects don't need them, larger projects or projects you intend to share with others benefit from using such tools.

Mistress answered 2/5, 2014 at 5:58 Comment(5)
Yes, JavaFX is just java, almost. If you are using fxml, this is a new xml resource that requires some infrastrucutre to utilize. This is different from a plain old vanilla Java SE application in small, but significant ways. One issue that I am having is that IntelliJ is not copying the fxml files from the src directories to the target directories, and therefore they aren't being found by getClass().getResource("MyFile.fxml"). Any insight is appreciated.Jasmin
Updated the answer based on your comment Noah.Mistress
@NoahTernullo unfortunately, now in 2017, the issue is still the same....Nutrient
@Fabio, all my Java projects in IntelliJ seem to default to !?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj, even if it's just a regular Java project.Dunlop
thanks now it builds with maven, but still doesn't build with JavaFXMolybdenum
J
1

One solution is to generate your code using Scene Builder, and then integrate it into your existing IntelliJ project. While it should be possible to import this as a module, you can also simply move the code to where it needs to be (making appropriate corrections to package statements, etc). If you notice that the javafx imports aren't recognized within your project, try this solution:

First ensure that you've set up IntelliJ in general for JavaFX by following the intial steps in this tutorial. Note: Below I mention 1.8, but this should work with 1.7+. I recommend 1.8+.

Second ensure that the existing project settings allow for javaFX use

  1. Open Project Structure
  2. Click on Project under the project settings pane
  3. Ensure that the Project SDK is 1.8+.

Third Ensure that the modules are configured for a JDK supporting JavaFX

  1. Open Project Structure
  2. Click on Modules under the project settings pane
  3. Ensure that the module SDK being used supports javaFX, Again I use JDK 1.8 but it should work with 1.7.

Also, please be aware that if you have a maven project, you may need to put your fxml in a particular location, "/main/resources/". Take a look at this.

That worked for me, and I hope it helps you.

Jasmin answered 13/8, 2014 at 16:18 Comment(0)
E
1

I followed those steps

  • Create a new module in your existing intellij project (A)
  • Create a new JavaFx project elsewhere
  • Copy the code from the JavaFx project, namely Controller.java,Main.java and sample.fxml into your module in the (A) project under the src directory as in A/user-interface/src

enter image description here

  • You'll find an error in sample.fxml since it references the controller class as sample.Controller change it to 'Controller' only. it worked this way (as I removed the sample package folder

Fixing sample.fxml

  • In (A) open the settings menu File | Project Structure | Modules
  • Select the user-interface module from the list on the left side of the settings window
  • Switch to the dependencies tab
  • Add your old non-gui module as a module dependency using the green + mark on the right-hand side
  • Now you can use the classes in the non-gui module, enjoy!

note: you can skip the add module dependency and let intellij do it for you, just type a name of a class in the old module, alt+enter (the light bulb magic) -> add module depenency

Module dependency window

Equilibrate answered 8/9, 2017 at 4:21 Comment(0)
M
1

So after lots of googling I finally found it out myself!

If you can create new Javafx projects in IntelliJ and it works perfectly, but when you open a normal java application that happens to use javafx doesn't work, (because java just doesn't want to resolve the javafx references)

what works for me is this: You must add the "jre/lib/ext/jfxrt.jar" file to the module classpath that is located inside IDEA on my windows machine this is C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\jre64\lib\ext\jfxrt.jar.

And now it finally builds!

Molybdenum answered 1/2, 2019 at 12:29 Comment(1)
This worked for me. To clarify ->file ->project structure->modules ->dependencies(tab onright) -> + (plus sign to add new) then select the file. I tried the accepted answer using a pom and new errors appeared. this was much easier.Entrance
P
0

Please keep in mind that your source(s) folder(s) has to be set correctly. See screenshot to get an idea what I mean..

enter image description here

Proser answered 24/10, 2015 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.