0

Lately I've started programming a game, and to switch scenes I want to incorporate JavaFX.

I've done extensive research to find out if I could use JavaFX in my project, and if so, how.

Could anyone give me some insight on this, and how I would be able to achieve this. If incorporating JavaFX to my Java code is not possible, could I get an alternative such as maybe creating a whole new JavaFX project and adding my code from my Java file.

An example of what I'm talking about:

(Action Listener) * When button is clicked *
{

(JavaFX takes in the variable name of the current JPanel and transitions to another JPanel)

}

I tried installing the JavaFX library to my NetBeans IDE and it worked. I haven't tried adding JavaFX to my project directly though.

What I'm thinking should happen is that it fits in with my code, as JavaFX is written in java. Through my research though, I learned that JavaFX is modular so in order to achieve my expectations, I'd have to create an all-new JavaFX project and import my code to it.

I'm unsure though and would like external input on this.

invzbl3
  • 5,872
  • 9
  • 36
  • 76
  • 1
    IMO the easiest thing for you do is to switch to a JDK that already includes JavaFX (Azul Zulu JDK FX or BellSoft Liberica Full JDK). Then you can write JavaFX code in your project and it will just work. Those distributions already include JavaFX in the base module system. If you don’t want to do that, then follow instructions at openjfx.io. – jewelsea Aug 18 '23 at 01:33
  • 1
    As you are using Swing with JavaFX, see the [Oracle JavaFX Swing](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm) tutorial, which is a little dated, but still mostly correct. – jewelsea Aug 18 '23 at 01:38
  • Creating `JavaFX` projects in `Netbeans` is very simple. See https://stackoverflow.com/a/73611206/2423906. Also see https://openjfx.io/openjfx-docs/ – SedJ601 Aug 19 '23 at 00:12

1 Answers1

3

JavaFX versus Swing

Your mention of JPanel makes me think you are currently using Swing as your GUI framework.

You need to understand that JavaFX is the successor to Swing. JavaFX is meant to replace Swing rather than complement.

  • Swing is a part of Java SE, bundled with every JDK. Oracle and the Java community will be supporting Swing for years to come. But the framework is in maintenance-mode, with no further feature work.
  • JavaFX is a collection of external libraries, not always bundled with a JDK (though Azul Systems and BellSoft both offer editions of their JDK with a JavaFX bundle). JavaFX is under continual development as the OpenJFX open-source project on the OpenJDK umbrella site. A JavaFX/OpenJFX version is released around the same time as every Java/OpenJDK release, every six months. OpenJFX is co-led by Oracle Corp and Gluon.

Also… JavaFX is not pure Java. The OpenJFX libraries contain native code, specific to a particular host platform.

Mixing JavaFX and Swing

You can mix some JavaFX content within a Swing app. Likewise, you can mix some Swing content within a JavaFX app. But there are issues and limitations in such mixing.

So if you are just starting an app, I would generally suggest picking one of the two frameworks and stick with it. The mixing is best for large apps that cannot afford a grand re-write and want to gradually transition from Swing to JavaFX.

To learn more about mixing JavaFX with Swing, start with this somewhat outdated tutorial by Oracle, JavaFX-Swing Interoperability.

Modularity

JavaFX is modular so in order to achieve my expectations, I'd have to create an all-new JavaFX project

Yes, JavaFX is modularized with the Java Platform Module System. This slightly complicates configuration. You'll need to have a short module-info.java file. You'll find some examples written by me and others right here on Stack Overflow. Plus your IDE may assist with fixing the module configuration (IntelliJ did help me).

Technically, you can rejigger your existing app to be modular. But personally, I’m lazy… I would start a new project with an already-modular app template.

New project

creating a whole new JavaFX project

If you decide to use JavaFX, I would suggest going this route. Start with a new JavaFX project, get it up and running, with compiles and builds working.

Drive your project with either Apache Maven or Apache Gradle. These dependency-management tools will automatically download and install the necessary JavaFX libraries (actually, OpenJFX libraries). And these tools will be also be very helpful when you later want to package your finished app with a bundled JVM via jlink & jpackage, and perhaps produce native code ahead-of-time compilation using GraalVM.

Caveat… To be frank, let me confess:

  • I have found the process of bundling a JVM with my JavaFX app to be quite complicated and frustrating. So far I have failed, and gave up for now. My current JavaFX app will have to be delivered to my first few users after installing a JVM bundled with the JavaFX libraries onto the users’ local machines.
  • As for getting the AOT compilation for native code, that too has also been a fail for me. But I am new to this, and these technologies are relatively new and cutting-edge.

Apparently others have had success, so I will be trying again. When I do, I will first try the explicit console commands shown by Kevin Rushforth in his 2023 presentation, JavaFX 20 and Beyond.

After your new Maven/Gradle-driven project is going, then bring over your game code from your old project. Depending on your existing code, this process may be quite easy. And, sage advice: Do so a bit at a time, to keep your new JavaFX project continuing to compile and run successfully at every step.

Maven/Gradle is more important than your IDE

in NetBeans IDE?

I expect you can find success with all this while using NetBeans, as Maven/Gradle work the same across all the IDEs including NetBeans, Eclipse, and IntelliJ.

Personally, I found starting a JavaFX app to be quite easy using the new-project template for JavaFX built into IntelliJ. But that is just my own taste. Any of the IDEs should work well.

screenshot of new project for JavaFX dialog box in IntelliJ

I tried installing the JavaFX library to my NetBeans IDE and it worked. I haven't tried adding JavaFX to my project directly though.

Sounds like you are using the NetBeans-native way of adding libraries to your project. I suggest you stop doing that soon, and instead learn to use Maven or Gradle as mentioned above. Those tools make easier the process of obtaining and installing libraries. And as I mentioned above, they can greatly assist with more complicated builds with tasks such as bundling a JVM, and producing AOT/native code.

The learning curve with Maven or Gradle is annoying at first, but well worth the time and effort. Many, if not most, professional Java developers utilize a dependency-manager and build-management tool like Maven, Gradle, or one of the few others.

Also, learning Maven or Gradle frees you from being too closely tied to the proprietary build system of a particular IDE. Using one of these independent build systems makes it easier for you to later migrate from one IDE to another.


Tip: Pay special attention to any JavaFX/OpenJFX posts on Stack Exchange written by jewelsea.

Tip: Be sure to study more recent materials on JavaFX. Focus on JavaFX 8 and later. The earlier versions went through dramatic evolution, so old materials may be irrelevant now.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Hello Basil. Thank you very much for the well explained response. I have now switched to IntelliJ IDE and found it to be much cleaner and easier to use. I'd like to ask though, when bring my code over to the JavaFX File, do I need to include the JavaFX imports or could I just leave my code raw? – Abraham Agbota Aug 18 '23 at 14:08
  • @AbrahamAgbota If you create a new project using the JavaFX template provided by IntelliJ, you will find at the top of the .java files both `module` and `import` lines. You’ll do similarly in your own files that you add to the project. – Basil Bourque Aug 18 '23 at 15:08
  • For reference, the project cited [here](https://stackoverflow.com/a/76276885/230513) illustrates using the `javafx-maven-plugin` option, [`javafx:jlink`](https://github.com/openjfx/javafx-maven-plugin#javafxjlink-options); see the project wiki and `nbactions.xml` for details. – trashgod Aug 19 '23 at 13:22
  • Alright, so by using this, would I be able to use the JPanel object in the JavaFX project? Because I know that JavaFX is different than JSwing. – Abraham Agbota Aug 19 '23 at 19:13