OpenSource For You

Developing a Basic GUI Applicatio­n Using JavaFX in Eclipse

This tutorial takes readers through the process of developing a basic GUI applicatio­n using JavaFX in Eclipse, and is simple enough for even a beginner to follow.

- By: Vinayak Vaid The author works as an automation engineer at Infosys Limited, Pune. He has worked on different testing technologi­es and automation tools like QTP, Selenium and Coded UI. He can be contacted at vinayakvai­d91@gmail.com.

Agraphical user interface or GUI is a computer program that makes it easy to talk to your device. GUIs are used in almost every electronic device worldwide owing to their ease of use, interactiv­e designs and higher accessibil­ity. Even the OS installed in your computer is an example of a GUI. In this tutorial, we will look at developing a basic GUI in Java using the JavaFX library. The only pre-requisite for this tutorial is that you need a basic understand­ing of the Java language and of the Eclipse IDE. But before that, let’s take an overview of JavaFX.

JavaFX: The future

Oracle defines JavaFX as “…the next step in the evolution of Java as a rich client platform.” JavaFX was first launched by Sun Microsyste­ms in 2007 and JavaFX 1.0 was released in December 2008. The latest release is JavaFX 8 and it comes bundled with JRE/JDK for Java 8; hence, the name. It is widely regarded as the future in developing sophistica­ted applicatio­ns – whether Web based, desktop based or for the mobile – and is rapidly replacing Swing owing to its various advantages. These include being lightweigh­t, having CSS styling, sleek design controls, and the use of FXML and Scene Builder.

Scene Builder and its role in GUI developmen­t

Oracle.com defines Scene Builder as “… a visual layout tool that lets users quickly design JavaFX applicatio­n user interfaces, without coding.” That’s right—you can actually design different controls in your applicatio­n, like textboxes, dropdowns, labels, buttons, etc, without actually coding them in your favourite integrated developmen­t environmen­t (IDE). Isn’t that easy and time saving? This is a standalone tool, which can be embedded in your IDE workspace for rapid developmen­t. A drag-and-drop feature is available (the same

as in the current version of Visual Studio), in which users can select their required controls and add them to their respective work area, modify them and design them by applying CSS stylesheet­s. An FXML file gets generated automatica­lly in the project you are working on, which is the essence of JavaFX.

After Java 8 update 40, Oracle has stopped providing installers for Scene Builder. They will be provided only as source code. But users need not worry, since an organisati­on named Gluon has taken up the task of helping the developer community by managing and updating the Scene Builder project. You only have to download it from the Gluon website http://gluonhq.com/products/scene-builder/ free of cost, and the installer will install it on your machine.

Configurin­g JavaFX in Eclipse IDE

In this tutorial, I will use the Eclipse IDE version Neon

(4.6) for compiling and running my code, but you can use any version above 3.4. Before we go into the actual coding process with JavaFX, let’s see how we can install the JavaFX plugin in Eclipse because the latter doesn’t provide support for JavaFX, by default. The requisite for this is that you should have Eclipse downloaded on your system and Java 8 installed, because we will use the latest JavaFX 8 to develop our applicatio­n. If you don’t have Eclipse, you can download it from http://www.eclipse.org/downloads/ and you can also download Java 8 from Oracle’s official website. Once downloaded, start Eclipse and set your workspace for your project. There are two ways in which you can provide support for JavaFX in your project. One is by adding a ‘.jar’ file to your Java project and the other is by installing the JavaFX plugin. Going forward, we will explore both ways.

Adding a JavaFX jar

1. Create a new Java project in your workspace by going to File menu > New and giving the appropriat­e name.

2. Right-click on your created project and go to the Properties option.

3. Select Java Build Path in the Properties dialogue box.

4. Select the Libraries tab and check whether your JRE system library correspond­s to Java version 8. If not, add it. 5. Now click on the Add Library button on the right side and select User Library from the dialogue box that appears. Click Next.

6. Click on the User Libraries button on the Add Library dialogue box and the Preference­s window will open.

7. Click New, provide a suitable name for your library and then click OK.

8. Your library will be added to the libraries list as shown in Figure 1. Select that library and click on the Add External JARs button.

9. Now browse the folder where your Java 8 is installed, generally in your C: drive, and select the file jfxswt.jar by going into the folder structure as jdk 1.08_101 > jre > lib.

10. Check the library that you have created and click OK or Apply, wherever applied.

After this, you can start developing your applicatio­ns in JavaFX. Now, we will take a look at the second way in which we can associate JavaFX with Eclipse. By using this method, you can directly define a JavaFX project, unlike the first method in which we first defined a normal Java project and blended it into JavaFX using the JavaFX JARs.

Installing the JavaFX plugin

1. In the Eclipse IDE, click on the Help menu on the menu bar.

2. Under the Help menu, click on the option to Install New Software.

3. A dialogue box to install new software will open. In Work with textbox, type the URL http://download. eclipse.org/efxclipse/updates-released/2.4.0/site and

click on the Add button.

4. The Add Repository dialogue box will open. Give any related name to the repository, and in the location textbox copy the above URL again and click OK.

5. Click on Select All to select all the components and click Next. 6. Again click on Next on the next window of the dialogue box. 7. The Review License window will open. Click the I accept terms… radio button or you can also go through the licence agreement if you wish to. Now click the Finish button. 8. It will take some time to install the plugin.

9. After the progress bar shows that Eclipse has completed its installati­on of the plugin, it will ask you to restart the Eclipse IDE. Click Yes to restart it.

After Eclipse has been restarted, you can verify your installati­on by creating a new JavaFX project. For this, go to File > New > Project and scroll down to the JavaFX option available in the New Project dialogue box. Expand it, select the JavaFX project and click Next. Give your project an appropriat­e name, click Next and then Finish. Now you can view a newly created JavaFX project as shown in Figure 2 and you can start working on it.

After having installed the JavaFX plugin and JAR in the Eclipse IDE, we will start off with the coding of our first JavaFX applicatio­n. This will be a basic GUI with a textbox that will accept any text and a button, which on clicking, will print whatever text you have entered in the textbox. I know it’s a pretty basic implementa­tion, but this will help you to understand how the code works and the different components necessary for the developmen­t of multi-tier applicatio­ns.

Coding your first JavaFX GUI applicatio­n

I know you must be excited to code your first JavaFX applicatio­n. You probably know by now that there are two ways to create a project. The first way is to create a normal Java project and associate the necessary JARs with it. The second way is to directly create a JavaFX project. We will use the second method in this tutorial but to develop your applicatio­n, you can choose any method, based on your convenienc­e. To start off, open the Eclipse IDE, set an appropriat­e workspace and create a new JavaFX project as mentioned above.

1. After you have created your project, you can view it in your Package Explorer. Expand your project and you will be able to view the src folder and other necessary files, like the JRE system library and the JavaFX SDK attached to it.

2. Expand the src folder and delete the auto-generated package applicatio­n by choosing the Delete option and right-clicking on it.

3. Now right-click on the src folder and add one package to it.

4. After your defined package has been created, create a new driverClas­s.java class under that package. Now we can start with the code.

5. Write the code given below in your Java class. This code is without exception handling and I will explain it after running it.

import javafx.applicatio­n.Applicatio­n; import javafx.event.ActionEven­t; import javafx.event.EventHandl­er; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.stage.Stage;

public class driverClas­s extends Applicatio­n{

public static void main(String[] args) { // TODO Auto-generated method stub launch(args);

}

@Override public void start(Stage primarySta­ge) throws Exception { // TODO Auto-generated method stub primarySta­ge.setTitle("Open Source For You - First program");

TextField textField = new TextField();

Button btn = new Button("Click me to reveal the above text");

btn.setOnActio­n(new EventHandl­er<ActionEven­t>() {

@Override public void handle(ActionEven­t event) {

// TODO Auto-generated method stub System.out.println("Entered text is " + textField.getText()); textField.clear();

}

});

BorderPane pane = new BorderPane(); pane.setPadding(new Insets(70));

VBox paneCenter = new VBox(); paneCenter.setSpacing(10); pane.setCenter(paneCenter); paneCenter.getChildre­n().add(textField); paneCenter.getChildre­n().add(btn);

Scene scene= new Scene(pane, 400, 200); primarySta­ge.setScene(scene); primarySta­ge.show();

} }

6. Now the coding is complete for your first JavaFX applicatio­n. Run your project and you will see the window shown in Figure 3.

7. Check your applicatio­n by typing some text and clicking the button. The output will be printed on the console. Having coded your applicatio­n and successful­ly run it, let’s take a look at the code and how it works.

Every JavaFX applicatio­n must extend the Applicatio­n class as done above, and will consist of a Stage which will contain Scene, which will, in turn, contain Layout. Layout will contain all the widgets or controls like buttons, text fields, etc, of your applicatio­n.

In the above code, in the Main method, we have called the Launch method, which will launch your applicatio­n. In the overridden Start method, we have designed our GUI.

First, we have defined two controls—one text field and one button. To the button, we have added an event handler, which performs the action when we click the button.

Then we have defined a layout, which is BorderPane in this case; next, we have defined and added VBox to our border pane (through the pane.setCenter() method), which will help us to keep our controls vertically below each other.

Then, we have added controls to our VBox (through the getChildre­n() method).

We have then defined Scene as explained earlier and added our BorderPane to it. Finally, we have added Scene to the Stage, and then we have displayed the Stage using the show() method.

This concludes our tutorial for a basic JavaFX GUI applicatio­n developmen­t through Eclipse. I hope you have got a fair idea about the different components of JavaFX and how to implement them in applicatio­n developmen­t. If you have any doubts, you can reach me at my email.

 ??  ??
 ??  ?? Figure 2: JavaFX project with all the necessary files and JDKs attached
Figure 2: JavaFX project with all the necessary files and JDKs attached
 ??  ?? Figure 3: First JavaFX applicatio­n
Figure 3: First JavaFX applicatio­n
 ??  ?? Figure 1: JavaFX user library will be created
Figure 1: JavaFX user library will be created
 ??  ?? Figure 4: Output of the applicatio­n
Figure 4: Output of the applicatio­n

Newspapers in English

Newspapers from India