Home » JavaFX with Eclipse

JavaFX with Eclipse

by Online Tutorials Library

JavaFX with Eclipse

Now, we need to configure Eclipse to execute the JavaFX applications. There are two ways of Eclipse Configuration for this purpose. We can either export the JavaFX jar files to every Java project or install a new software which can support the JavaFX project creation directly.

In this part, we will look at both the ways. First Lets look at the way by which, we can export the JavaFX jar files to the Java Project.

Exporting JavaFX jar files to the Java Project

Just click on the file icon appeared at the top left corner of the eclipse screen. Click on new and select Java project from the various types of options given in the list.

JavaFX with Eclipse Exporting JavaFX jar files

The following Pop-up window will be opened. Enter the project details like project name and click Finish.

JavaFX with Eclipse Exporting JavaFX jar files 1

Now, we have created our project. The project doesn’t support the JavaFX syntax. We need to export the JavaFX jar files to the project in order to run the JavaFX application.

Just Right click on the project and select properties from the options.

JavaFX with Eclipse select properties

Go to Java Build Path → Libraries. Here, the JRE System Library is shown which is responsible to execute our java project. We need Add a Library here, choose Add Library from the options in the list given on the right side of the screen.

JavaFX with Eclipse Java Build Path Add Library

Choose User Library from the list and click Next to continue.

JavaFX with Eclipse User Library
JavaFX with Eclipse User Library 2

Now, We are in the user library section. We need to create a new User Library hence choose New from the list given on the right side of the system.

JavaFX with Eclipse User Library choose New

Just enter JavaFX in the user library name and press OK.

JavaFX with Eclipse User Library press OK

Now, a user library named as JavaFX has been created, we need to add some external JARs file here to make our project working.

Click on Add External JARs from the list given on the right side of the system and choose appropriate JAR files from the system.

JavaFX with Eclipse appropriate JAR

browse to Java/jre1.8.0_161/lib in the system and select jfxswt.ja rpresent in the directory. However, to set-up the appropriate environment for JavaFX application, we need to add one more JAR file so just browse to Java/jre1.8.0_161/lib/ext and choose jfxrt.jar.

JavaFX with Eclipse appropriate JAR 2

Now, we are all ready to execute our first class file of the project. We have added the number of JAR files ( shown in the image below) to our project. Click on Apply and Close to apply the settings to the project.

However, this is valid for this project only and we have to repeat this whole procedure with the other projects as well. It is not the better idea to implement but we can execute the JavaFX applications for now.

JavaFX with Eclipse Apply and Close
JavaFX with Eclipse Apply and Close 2

Right click on the project and follow New → Class to create your first class of the application. Copy and paste some JavaFX code and run the application. We will get the following output from our first JavaFX application which contains a button which prints hello world on pressing it.

JavaFX with Eclipse hello world

We have just executed our first JavaFX application by exporting JAR files to the project.

Now, we will discuss how we can install a new software to eclipse in order to create a JavaFX project directly.

Install a New Software

Open Eclipse and click on Help. Choose Install a New Software from the list of options given in the drop down menu.

JavaFX with Eclipse Install a New Software

Now, we need to add a repository, just click Add. Type e(fx)clipse in the Name and type http://download.eclipse.org/efxclipse/updates-released/1.2.0/site/in the location. Press OK to proceed with this repository installation.

JavaFX with Eclipse updates released

Now, the name of all the components which will be installed from the location which we have just entered. Click on Select All and click Next.

JavaFX with Eclipse updates released installed

A list has been shown which contains all the components which will be installed. Just click Next to continue with the installation.

JavaFX with Eclipse updates released installed 2

Now, a software agreement has been shown. Just select the radio box which says I accept the terms and conditions and click finish.

JavaFX with Eclipse updates released installed terms and conditions

Now, the software is being installing to the eclipse. Once the process gets over, it shows a pop-up window which asks to restart the eclipse. Just click Restart Now and wait while restart process gets done.

JavaFX with Eclipse updates released installed terms and conditions 2

Now, we have installed the new software which enables us to create the new JavaFX Application directly. We just need to click on file and choose new → project from the drop down options.

JavaFX with Eclipse installed the new software enable us

Now, we can see that there is a option of JavaFX project. We can now create JavaFX application directly. Choose JavaFX Project and click Next.

JavaFX with Eclipse installed the new software enable us2

Enter the JavaFX project details and click Finish

JavaFX with Eclipse install project details Finish

Now, create your first JavaFX class. Just enter the class name in the pop-up window shown below. Click Finish.

JavaFX with Eclipse install project details Finish 2

Paste some JavaFX code into the file and execute the class file.

JavaFX with Eclipse execute the class file

We got our application running fine, printing hello world on the console on clicking the “Say Hello Button”

JavaFX with Eclipse execute the class file Say Hello Button

Hence, we have created an environment in which, we can execute our JavaFX application in eclipse.

You may also like