Chapter 1 JAVAfx
Chapter 1 JAVAfx
JavaFX: Introduction
JavaFX is a collection of Java packages that lets
you add fancy graphical user interfaces to your
Java applications.
JavaFX provides a rich set of graphics and media
API’s and it leverages the modern Graphical
Processing Unit through hardware accelerated
graphics.
JavaFX also provides interfaces using which
developers can combine graphics animation and UI
control.
It allows you create traditional windows-style user
interfaces that include familiar controls such as:
labels,
buttons,
textboxes,
check boxes,
drop-down lists, and so on.
FXML
JavaFX features a language known as FXML, which is a HTML like
declarative markup language.
The main purpose of this language is to define a user Interface.
Scene Builder
JavaFX provides an application named Scene Builder.
Swing Interoperability
In a JavaFX application, you can embed Swing content using
the Swing Node class.
Similarly, you can update the existing Swing applications with
JavaFX features like embedded web content and rich graphics
media.
Built-in UI controls
JavaFX library caters UI controls using which we can develop a full-featured
application.
CSS like Styling
JavaFX provides a CSS like styling which will help you improve the design
of your application
Canvas and Printing API
JavaFX provides Canvas, an immediate mode style of rendering API.
Within the package javafx.scene.canvas it holds a set of classes for
canvas, using which we can draw directly within an area of the JavaFX
scene.
JavaFX also provides classes for Printing purposes in the
package javafx.print.
Integrated Graphics library
JavaFX provides classes for 2d and 3d graphics
Architecture
There are set of classes and interfaces which are used to build GUI
applications using JavaFX API
javafx.animation
Contains classes to add transition based animations such as fill,
fade, rotate, scale and translation, to the JavaFX nodes.
javafx.application
Contains a set of classes responsible for the JavaFX application
life cycle.
javafx.css
Contains classes to add CSS–like styling to JavaFX GUI
applications.
javafx.event
Contains classes and interfaces to deliver and handle JavaFX
events.
javafx.geometry
Contains classes to define 2D objects and perform operations on
them.
javafx.stage
the top level container classes for JavaFX application.
javafx.scene
provides classes and interfaces to support the scene graph.
Stage:
Is also referred to as window
All JavaFX application should be contained in Stage
It is JavaFX applications’ highest-level window
Is represented by Stage class in javafx.stage package
A primary stage will be created by the platform you are using
As in Swing, you have to call the show() method to display the
contents of a stage
Scene:
It is physical content of JavaFX application
Contains all the contents of a scene graph
All UI elements will be included in Scene
Is represented by the Scene class of javafx.scene package.
At a single instance, the scene object is added to only one stage.
Nodes:
Is a visual object of a scene graph
Scene Graph: is hierarchical data structure representing the contents of
a scene
May include Geometrical objects, UI controls, Containers, Media
elements
Is represented by Node class of javafx.scene package
A Node could be
Root,
The first Scene Graph is the Root Node
Branch/ Parent
Nodes, that are not root, but have child nodes are Parent Nodes
Could be
Group: is a collective node that contains a list of children nodes
Any effect applied to the group will also be applied to all the child
nodes
Region: base class of Node based UI Controls, such as Pane, and
Control
WebView: manages the web engine and displays is content
Leaf
Node with no child
Example: ImageView, Box, Rectangle
Getting Started…
To create JavaFx programs, you need to import some packages. The
followings are common:
javafx.application: defines the core class on which all JavaFX
applications depend: i.e. The Application Class
javafx.stage: defines Stage class.
This method is where you are going to write the code for your
JavaFX Application.
Preparing a Scene
Preparing a stage
Alternatively, you can pass the node object to a Group class upon instance.
Region: is the Base class of all the JavaFX Node-based UI Controls,
such as
Chart
Example:
public void start(Stage primaryStage)
{
primaryStage.setScene(scene);
primaryStage.setTitle(“My First JavaFx App");
primaryStage.show();
}
Launching the application
Recall, main() method is standard entry point for Java programs
If you need, you can turn this behavior off by passing the Boolean
value “False” to the static method setImplicitExit()
FileChooser A dialog window from which the user can select a file
ProgressBar Displays tasks completion percentage
Slider lets the user graphically select a value by sliding a knob within a bounded
interval
ColorPicker Provides a pane of controls designed to allow a user to manipulate and select a
color
Menu Implements menus
Label
Represented by javafx.scene.control.Label class
Label is the component that is used to place any text information on
the screen.
It is mainly used to describe the purpose of the other components to
the user.
Note: You can not set a focus on the label using the Tab key.
Constructors:
Label()
Label(String text)
Label(String text, Node graphics)
Example :
Example:
FileInputStream input=new FileInputStream(“imageURL");
Image image = new Image(input);
ImageView img=new ImageView(image);
rad1.setToggleGroup(group);
rad2.setToggleGroup(group);
CheckBox
The Check Box is used to provide more than one choices to the
user.
Example:
CheckBox c1 = new CheckBox(“Java");
CheckBox c2 = new CheckBox(“Python");
HyperLink
You can use hyper-links to refer the web pages.
It is similar to anchor links in HTML
Constructors
HyperLink hyp1= new HyperLink()
HyperLink hyp1= new HyperLink(String text)
HyperLink hyp1= new HyperLink(String text, Node graphic)
Example
You can use getValue() method of the slider to get the actual
value of the Slider
ProgressBar
It is used to show the work progress to the user.
BorderPane Organizes nodes in Top, Left, Right, Centre and the Bottom of the screen.
FlowPane Organizes the nodes in the horizontal rows according to the available
horizontal spaces.
Wraps the nodes to the next line if the horizontal space is less than the
total width of the nodes
StackPane Organizes nodes in the form of a stack i.e. one onto another
Example:
BorderPane bp= new Borderpane();
Bp.setBottom(new Label(“label at Bottom”));
HBox
Constructors:
HBox() : create HBox layout with 0 spacing
StackPane(Node Children)