0% found this document useful (0 votes)
11 views11 pages

Java FX

JavaFX is a Java library and GUI toolkit for developing Rich Internet Applications, web, and desktop applications, designed to provide a modern, hardware-accelerated user interface. It includes features such as FXML for enhanced GUI, Scene Builder for generating FXML, and a rich set of APIs for both 2D and 3D graphics. JavaFX applications follow a structured architecture and utilize various UI controls and layouts to create interactive user experiences.

Uploaded by

Vijay Ajay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Java FX

JavaFX is a Java library and GUI toolkit for developing Rich Internet Applications, web, and desktop applications, designed to provide a modern, hardware-accelerated user interface. It includes features such as FXML for enhanced GUI, Scene Builder for generating FXML, and a rich set of APIs for both 2D and 3D graphics. JavaFX applications follow a structured architecture and utilize various UI controls and layouts to create interactive user experiences.

Uploaded by

Vijay Ajay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVAFX

BASIC CONCEPTS:
⁕ JavaFX is a Java library and a GUI toolkit designed to develop and facilitate Rich Internet
applications, web applications, and desktop applications.
⁕ JavaFX allows you to create Java applications with a modern, hardware-accelerated user
interface that is highly portable.
⁕ JavaFX was introduced to supersede the Java Swing GUI framework.
⁕ JavaFX is considered to be lightweight and hardware stimulated.
⁕ JavaFX is a Java library used to develop Desktop applications as well as Rich Internet
Applications (RIA). The applications built in JavaFX, can run on multiple platforms including
Web, Mobile and Desktops.

History of JavaFX
JavaFX was developed by Chris Oliver. Initially the project was named as Form Follows
Functions (F3) . It is intended to provide the richer functionalities for the GUI application
development. Later, Sun Micro-systems acquired F3 project as JavaFX in June, 2005.
Sun Micro-systems announce it officially in 2007 at W3 Conference. In October 2008, JavaFX
1.0 was released. In 2009, ORACLE corporation acquires Sun Micro-Systems and released JavaFX 1.2.
the latest version of JavaFX is JavaFX 1.8 which was released on 18th March 2014.

Features of JavaFX :

It is a Java library which consists of many classes and interfaces


Java Library
that are written in Java.

FXML is the XML based Declarative mark up language. The


FXML coding can be done in FXML to provide the more enhanced GUI
to the user.

Scene Builder generates FXML mark-up which can be ported to


Scene Builder
an IDE.

Web pages can be embedded with JavaFX applications. Web


Web view
View uses WebKitHTML technology to embed web pages.
JavaFX contains Built-in components which are not dependent
Built in UI controls on operating system. The UI component are just enough to
develop a full featured application.

JavaFX code can be embedded with the CSS to improve the style
CSS like styling of the application. We can enhance the view of our application
with the simple knowledge of CSS.

The JavaFX applications can be embedded with swing code using


Swing interoperability the Swing Node class. We can update the existing swing
application with the powerful features of JavaFX.

Canvas API provides the methods for drawing directly in an area


Canvas API
of a JavaFX scene.

Rich Set of APIs JavaFX provides a rich set of API's to develop GUI applications.

An integrated set of classes are provided to deal with 2D and 3D


Integrated Graphics Library
graphics.

JavaFX graphics are based on Graphics rendered pipeline(prism).


Graphics Pipeline
It offers smooth graphics which are hardware accelerated.

High Performance Media The media pipeline supports the playback of web multimedia on
Engine a low latency. It is based on a Gstreamer Multimedia framework.

Self-contained application Self Contained application packages have all of the application
deployment model resources and a private copy of Java and JavaFX Runtime.
Architecture of JavaFX

1. JavaFX API – The topmost layer of JavaFX architecture holds a JavaFX public API that
implements all the required classes that are capable of producing a full-featured JavaFX
application with rich graphics.
2. [Link]: It includes classes that are used to combine transition-based animations
such as fill, fade, rotate, scale and translation, to the JavaFX nodes (collection of nodes
makes a scene graph).
3. [Link] − It comprises classes that are used to append CSS–like styling to the JavaFX GUI
applications.
4. [Link] − It contains classes that are used to represent 2D figures and execute
methods on them.
5. [Link] − This package of JavaFX API implements classes and interfaces to establish the
scene graph. In extension, it also renders sub-packages such as canvas, chart, control, effect,
image, input, layout, media, paint, shape, text, transform, web, etc.
6. [Link] − This package includes a collection of classes that are responsible for the
life cycle of the JavaFX application.
7. [Link] − It includes classes and interfaces that are used to perform and manage JavaFX
events.
8. [Link] − This package of JavaFX API accommodates the top-level container classes
used for the JavaFX application.
9. Scene Graph – A Scene Graph is the starting point of the development of any of the GUI
Applications.
10. Quantum Toolkit – Quantum Toolkit is used to connect prism and glass windowing tool kits
collectively and makes them prepared for the above layers in the stack.
11. Prism – The graphics of the JavaFX applications are based on the hardware-accelerated graphics
rendering pipeline, commonly known as Prism.
12. Glass Windowing Toolkit – Glass Windowing Toolkit or simply Glass is a platform-dependent
layer that assists in connecting the JavaFX platform to the primary operating system (OS).
13. WebView – WebView is used to make it possible to insert web pages within a JavaFX
application. JavaScript appearing in WebView can call Java APIs and vice-versa. This element
promotes different web technologies like HTML5, CSS, JavaScript, DOM, and SVG. Using web view,
we can execute the HTML content from the JavaFX application and can also implement some CSS
styles to the user interface (UI) part of the application.
14. Media Engine – Like the graphics pipeline, JavaFX also possesses a media pipeline that advances
stable internet multimedia playback at low latency.
Basic Program for Printing a sentence:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DrawLineExample extends Application {
@Override
public void start(Stage primaryStage) {
// Create a Pane to hold the line
Pane pane = new Pane();
// Create a Line
Line line = new Line();
// Set the start (x1, y1) and end (x2, y2) points of the line
[Link](50); // x1
[Link](50); // y1
[Link](200); // x2
[Link](150); // y2
// Add the line to the pane
[Link]().add(line);
// Create a Scene with the pane, set its size
Scene scene = new Scene(pane, 300, 200);
// Set the title of the window (Stage)
[Link]("JavaFX Line Example");
// Add the scene to the stage and show it
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX Transformations
Transformation implies modifying some graphics into something different by implementing
various rules. In JavaFX there are different sorts of transformations such as Translation, Scaling Up or
Down, Rotation, Shearing, etc.
Using JavaFX, a user can implement transformations on nodes such as rotation, scaling, and
translation. All these transformations are expressed by several classes and these classes belong to
the package [Link].

Different classes of JavaFX Transformations


In JavaFX, there are basically 4 predefined JavaFX transformations classes that are available
for the user to use in their application. The following is a brief introduction of various
transformations that are provided by JavaFX.

● Rotation – Rotation is used to rotate the object from its source by a certain angle. The
class [Link] represents rotation.
● Translation – Translation is used to modify the location of the node. The
class [Link] represents the translation.
● Scaling – Scaling is used to modify the size of the node. The
class [Link] represents Scaling.
● Shearing – Shearing is used to altering the slope of the object in a particular direction. The
class [Link] represents Shearing.
Steps to apply transformations to the target node are as follows:
To apply a special transformation to a target node, a user should follow the below-given
steps:

1. The first step is to instantiate the class of the respective transformation which the user wants to
apply.
For example – If a user wants to apply a translate transformation on a specific node, then he
should instantiate the translate class.
TranslateTransition translate = new TranslateTransition();
2. After instantiating the class, the next step is to set of properties of the instantiated class.
For example – If a user has instantiated the translate transformation class of JavaFX, then he
should set the properties such as translate byX, translate byY, duration, and cycle count of
the translated class.
[Link](400);
[Link]([Link](1000));
[Link](500);
3. Set the transformation to the respective node and play the transformation using the method
play().
For Example: Let us assume that the user wants to translate a specific node then after
instantiating a transformation class and setting its value, the user should set the node using
the name of the node that should play the translation.
[Link](name of the node);
[Link]();

JavaFX 3D Shapes
Any solid geometrical figure that can be represented on the coordinate system using three
planes, which are X, Y, and Z planes, is known as a three-dimensional shape or a 3D shape. The class
Shape3D of the package [Link] is the base class of all the 3D shape classes in javafx.
Types of 3D shapes in JavaFX
In JavaFX, 3D shapes are characterized in two separate types. These are:
1. Predefined 3D shapes: Predefined 3D shapes are the shapes whose classes are already present in
the JavaFX API. These shapes are Cylinder, Sphere, and Box.
2. User Defined 3D shapes: JavaFX provides the class [Link] which
extends the abstract class [Link]. This class facilitates the user to define their
own points, texture coordinates, and faces as the properties of the class.
Steps to create a 3D shape :

1. The first step is to instantiate the class of the respective 3D shape which the user wants to build.
For Example – If a user wishes to create a Box, then he should instantiate the Box class of
the JavaFX.
box = new Box();
2. After instantiating the class, the next step is to set of properties of the instantiated class.
For Example – If a user has instantiated the Box class of JavaFX, then he should set the
properties such as Height, width, and depth of the Box class.
[Link](100.0);
[Link](80.0);
[Link](60.0);
3. After the above two steps, the user should set the camera for the scene. In simple words, the
user can set the camera to a particular position for the camera view. However, this step is optional.
A user can set the camera view using the following code –
PerspectiveCamera cam = new PerspectiveCamera();
[Link](100.0);
[Link](80.0);
[Link](-50.0);
[Link](cam)
4. The last step to creating a 3D shape for the JavaFX application is to attach the 3D shape to the
Scene graph and set the relevant properties for the Scene and stage.
For example: If a user has instantiated the box class, then he should link that class with the
Scene graph and the stage.
Group root = new Group();
[Link]().add(box);
Scene scene = new Scene(root,500,500);
[Link](scene);
[Link]("3D shape Example");
[Link]();
JavaFX UI Controls
The UI controls are the elements that are truly exposed to the user for intercommunication
or information exchange. A layout represents the structure of the UI elements on the screen.
Different classes of JavaFX UI controls
In JavaFX, there are many UI controls that are used by the users in their applications. Below
is the list of usually used UI controls while the GUI is designed using JavaFX.

● Button – The button is a component that controls the function of the application. The Button
class is practiced to produce a specified button. It is represented by
the [Link] class.

● Label – Label is an element that is applied to describe plain text on the screen. Typically, a
label is set with the node, it represents. It is represented by
the [Link] class.

● TextField – Text Field is usually used to take the input from the user in the form of text. It is
represented by the [Link] class.

● ProgressBar – Progress Bar is used to display the output process to the user. It is represented
by the [Link] class.

● RadioButton – The Radio Button is used to provide various options to the user. The user can
only choose one option among all. A radio button has two options, it can either be selective or
deselective. It is represented by the [Link] class.

● CheckBox – Check Box is used to get the kind of information from the user which contains
various choices. The user marked the checkbox either on (true) or off(false). The CheckBox
in JavaFX is expressed by the [Link] class.

● Slider – Slider is used to provide a pane of options to the user in a graphical form where the
user needs to move a slider over the range of values to select one of them. It is represented by
the [Link] class.

● Menu – JavaFX provides a Menu class to implement menus. The menu is the main
component of any application. It is represented by the [Link] class.

● PasswordField – PasswordField is practiced to take the user’s password. Whatever the user
types in the password field, that is not displayed on the screen to anyone. It is expressed by
the [Link] class.

● ScrollBar – JavaFX Scroll Bar is used to implement a scroll bar to the user so that the user
can scroll down the application pages. It is represented by
the [Link] class.
Example Program for JavaFX Controls:

import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];

public class JavaFXControlsExample extends Application {

@Override
public void start(Stage primaryStage) {
// Create controls
Label nameLabel = new Label("Name:");
TextField nameField = new TextField();
Label ageLabel = new Label("Age:");
TextField ageField = new TextField();
CheckBox subscribeBox = new CheckBox("Subscribe to newsletter");
Button submitButton = new Button("Submit");
// Create label to display results
Label resultLabel = new Label();
// Set button action
[Link](e -> {
String name = [Link]();
String age = [Link]();
boolean isSubscribed = [Link]();
String subscriptionText = isSubscribed ? "subscribed" : "not subscribed";
[Link]("Hello, " + name + ". Age: " + age + ". You are " + subscriptionText + " to
the newsletter.");
});
// Create layout and add controls
VBox layout = new VBox(10);
[Link]().addAll(nameLabel, nameField, ageLabel, ageField, subscribeBox,
submitButton, resultLabel);

// Set scene and stage


Scene scene = new Scene(layout, 300, 200);
[Link]("JavaFX Controls Example");
[Link](scene);
[Link]();
}

public static void main(String[] args) {


launch(args);
}
}

Application Development in JavaFX


JavaFX is a powerful framework for building modern desktop applications with rich
graphical user interfaces (GUIs). It allows developers to create highly interactive applications with
advanced UI controls, multimedia, animation, and CSS-based styling.
1. JavaFX Application Structure
Every JavaFX application follows a similar structure, which revolves around the Application
and the start() method.
2. JavaFX Components (UI Controls)
offers a variety of UI controls that are essential for building interactive applications. Common UI
components include:
● Buttons: For triggering actions.
● TextFields: For user input.
● Labels: To display static or dynamic text.
● CheckBoxes/RadioButtons: For selection options.
● ComboBox: Drop-down list for user selections.
● TableView: To display data in table format.
3. Layouts in JavaFX
Layouts are essential for organizing UI components. JavaFX provides several layout managers:
● VBox: Arranges components vertically.
● HBox: Arranges components horizontally.
● GridPane: Arranges components in a grid format.
● BorderPane: Divides the scene into five regions: top, bottom, left, right, center.
● StackPane: Stacks components on top of each other.

4. Styling JavaFX Applications

JavaFX allows you to style applications using CSS. You can apply styles to individual
components or entire scenes.

5. Multimedia and Animation in JavaFX


JavaFX supports multimedia content such as images, audio, and video. You can also create
animations to enhance the user experience.
6. Event Handling in JavaFX
JavaFX uses event-driven programming. You can add event listeners to respond to user
actions such as button clicks, key presses, etc.
7. JavaFX Scene Builder
JavaFX Scene Builder is a visual tool that helps developers design UI components
graphically. You can drag and drop UI elements, customize them, and automatically generate
FXML files.
● FXML: JavaFX supports XML-based UI definition via FXML, allowing separation of design
and logic.
8. JavaFX Project Structure
● src: Source code folder (Java files).
● resources: Resources folder (CSS, images, FXML files).
● [Link]: The main entry point of the application.
9. JavaFX Deployment
JavaFX applications can be packaged as:
● Executable JAR: A self-contained JAR file.
● Native installers: Installers for platforms like Windows, macOS, and Linux.
● Web Deployment: Through Java Web Start.

You might also like