Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Using JavaFX with Eclipse: A Step-by-Step Guide, Study notes of Computer Science

A comprehensive guide on how to use javafx with eclipse, including downloading the javafx sdk, installing e(fx)clipse, creating an eclipse java project that uses javafx, and configuring the run configuration for javafx applications. The guide includes screenshots and detailed instructions for macos and linux users.

Typology: Study notes

2023/2024

Uploaded on 04/22/2024

ethan-cooper-2
ethan-cooper-2 🇺🇸

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Using JavaFX with Eclipse
Download JavaFX SDK
Start by downloading the JavaFX SDK library, using the directions here:
https://gluonhq.com/products/javafx/
There you will have several possible downloads. You want to choose the one
that matches your platform. For MacOS, if you have Apple's M1 or M2 CPU,
you'll want the "aarch64" version, otherwise the "x64" version. (I've
highlighted the M1/M2 button, below.) Linux users have a similar choice—use
"x64" if you are using an Intel CPU.
The download will yield a zip file, which you should expand. Move the JavaFX
SDK folder to someplace memorable, like your home directory, or (if you are
a Windows person) the C: drive. For example, on my Mac I moved it to a
folder named bin in my home directory. Take note of the pathname of the
location of the new folder. For example, on my Mac it's at
/Users/matt/bin/javafx-sdk-21.0.2.
Install e(fx)clipse
Next, in Eclipse, use HelpEclipseMarketplace to install the addon
e(fx)eclipse.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Using JavaFX with Eclipse: A Step-by-Step Guide and more Study notes Computer Science in PDF only on Docsity!

Using JavaFX with Eclipse

Download JavaFX SDK

Start by downloading the JavaFX SDK library, using the directions here: https://gluonhq.com/products/javafx/ There you will have several possible downloads. You want to choose the one that matches your platform. For MacOS, if you have Apple's M1 or M2 CPU, you'll want the "aarch64" version, otherwise the "x64" version. (I've highlighted the M1/M2 button, below.) Linux users have a similar choice—use "x64" if you are using an Intel CPU. The download will yield a zip file, which you should expand. Move the JavaFX SDK folder to someplace memorable, like your home directory, or (if you are a Windows person) the C: drive. For example, on my Mac I moved it to a folder named bin in my home directory. Take note of the pathname of the location of the new folder. For example, on my Mac it's at /Users/matt/bin/javafx-sdk-21.0.2.

Install e(fx)clipse

Next, in Eclipse, use HelpEclipseMarketplace to install the addon e(fx)eclipse.

In the Marketplace, search for "fx":

You'll probably have to restart Eclipse as part of the installation of the addon.

Create an Eclipse Java Project that Uses JavaFX

[Much of the following is adopted from https://www.javatpoint.com/javafx- with-eclipse] Create a new Java project in Eclipse. You should be using a JRE equal to or newer than JRE11. (I'm using JRE17 in this document.) For simplicity, use a single folder to hold the source and class files, and DO NOT enable the module-info.java file by leaving the corresponding checkbox unmarked:

In Eclipse we need to create an Eclipse library that contains the JavaFX stuff. The contents of the library already exist, but we have to define it. There are a number of ways to do this, but here's one: right-click on the project folder and use the BuildPathConfigureBuildPath command.

Click on that and you should see something like the image below (ignore the JavaFX SDK library, if it's there). Select User Library and click the Next button.

You should see something like below, but without the JavaFX21. Click on User Libraries. You'll get something like below (still without the "JavaFX21"). Click on New… You'll get something like below. Give the library a name. Here I've called it "JavaFX21.1", but you could just use "JavaFX".

Once you are viewing that directory, select all the jar files and click Open. This will return you to the User Libraries window, shown below, where you should click "Apply and Close":

You should find yourself back in the "Add Library" window with your newly created library now visible. Make sure you've selected it (see the checkmark icon):

Eclipse is complaining because it can't find the imports at lines 3-6. Those classes are defined in the jar files we looked at previously and placed into our user library. If I know go ahead and alter the project's Java Build Path to include the Eclipse user library we defined previously (JavaFX21.1), all those nasty red X icons should go away: So now just run the program, right? Sadly, no. I right-click on MyJavaFX.java in the Package Explorer window and try to run it as an application:

But I get this error message in the Console window in Eclipse: We need to tweak the "run configuration". A run configuration tells Eclipse how to execute the application. JavaFX applications don't have a "main" method. Rather we have to tell the JRE which method to call first. We do that via a command line parameter. If you run Java programs from the command line you do this kind of thing all the time. Normally, in Eclipse, these details are hidden from you. Now, though, we need to tell Eclipse to use a particular command line parameter. There are a couple of ways to do this, but here's my recommendation. You'll be repeating this technique each time we want to run a different program that uses JavaFX.

  1. Try to run the driver (it was MyJava.java, above). This will fail, as we just saw, but it will create a new Eclipse Run Configuration, which we will then tweak.
  2. Try to run it again, but this time select Run Configurations…
  3. The default Run Configuration created as step 1 will be selected. Click on the Arguments tab:
  1. The application should run: Each time you want to run a new JavaFX application, you'll go through steps 1-6, above. Once you've created the run configuration for the application, though, you need only run it as a regular application—no need to create a new run configuration:

Notes Liang's supplement video on Eclipse and JavaFX in Windows: https://www.youtube.com/watch? v=qY5Zh1bBIts Referenced from Student Supplements in his textbook. Using JavaFX with Eclipse https://openjfx.io/openjfx-docs/#install-javafx Used non-modular approach.