Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

JAVAFX Dr.B.

Sathis
Kumar
 GUI programming using JavaFX, Exploring events, controls and
JavaFX menus
WHAT IS JAVAFX?

 JavaFX is a set of graphics and media packages that enables


developers to design, create, test, debug, and deploy rich client
applications that operate consistently across diverse platforms.
 JavaFX is a Java library used to build Rich Internet
Applications. The applications written using this library can run
consistently across multiple platforms. The applications
developed using JavaFX can run on various devices such as
Desktop Computers, Mobile Phones, TVs, Tablets, etc..
 To develop GUI Applications using Java programming language,
the programmers rely on libraries such as  Advanced Windowing
Tool kit and Swing. After the advent of JavaFX, these Java
programmers can now develop GUI applications effectively with
rich content.
JAVAFX APPLICATION STRUCTURE
STAGE

 A stage (a window) contains all the objects of a JavaFX


application.
 It is represented by Stage class of the package javafx.stage.
 The primary stage is created by the platform itself.
 The created stage object is passed as an argument to
the start() method of the Application class
 A stage has two parameters determining its position
namely Width and Height. It is divided as Content Area and
Decorations (Title Bar and Borders).
 There are five types of stages available −
 Decorated
 Undecorated
 Transparent
 Unified
 Utility
 You have to call the show() method to display the contents of
a stage.
SCENE

 A scene represents the physical contents of a JavaFX


application. It contains all the contents of a scene graph. The
class Scene of the package javafx.scene represents the scene
object.
SCENE GRAPH AND NODES

 A scene graph is a tree-like data structure (hierarchical)


representing the contents of a scene. In contrast, a  node is a
visual/graphical object of a scene graph.
 A node may include −
 Geometrical (Graphical) objects (2D and 3D) such as − Circle,
Rectangle, Polygon, etc.
 UI Controls such as − Button, Checkbox, Choice Box, Text
Area, etc.
 Containers (Layout Panes) such as Border Pane, Grid Pane,
Flow Pane, etc.
 Media elements such as Audio, Video and Image Objects
SCENE GRAPH AND NODES

 The class named GridPane of the


package javafx.scene.layout represents the GridPane. This
class provides eleven properties, which are −
 alignment − This property represents the alignment of the
pane and you can set value of this property using
the setAlignment() method.
 hgap − This property is of the type double and it represents
the horizontal gap between columns.
 vgap − This property is of the type double and it represents
the vertical gap between rows.
 gridLinesVisible − This property is of Boolean type. On true,
the lines of the pane are set to be visible.
APPLICATION CLASS

 The Application class of the package javafx.application is the


entry point of the application in JavaFX. To create a JavaFX
application, you need to inherit this class and implement its
abstract method start(). In this method, you need to write the
entire code for the JavaFX graphics
 In the main method, you have to launch the application using
the launch() method. This method internally calls
the start() method of the Application class as shown in the
following program.
JAVAFX CODE TEMPLATE

public class JavafxSample extends Application {


@Override
public void start(Stage primaryStage) throws Exception {
/*
Code for JavaFX application.
(Stage, scene, scene graph)
*/
}
public static void main(String args[]){
launch(args);
}
}
LIFECYCLE OF JAVAFX APPLICATION

 The JavaFX Application class has three life cycle methods,


which are −
 start() − The entry point method where the JavaFX graphics
code is to be written.
 stop() − An empty method which can be overridden, here you
can write the logic to stop the application.
 init() − An empty method which can be overridden, but you
cannot create stage or scene in this method.
BUTTON DESIGN USING JAVAFX DEMO

 SimpleButton.Java
SIMPLE FORM DESIGN USING JAVAFX
DEMO
 SimpleForm.java
CELL POSITIONS IN THE GRID PANE OF JAVAFX −

(0, 0) (1, 0) (2, 0)

(0, 1) (1, 1) (2,1)

(2, 2)
(0, 2) (1, 2)
EVENTS

 In JavaFX, we can develop GUI applications, web applications


and graphical applications. In such applications, whenever a
user interacts with the application (nodes), an event is said to
have been occurred.
 For example, clicking on a button, moving the mouse,
entering a character through keyboard, selecting an item from
list, scrolling the page are the activities that causes an event
to happen
TYPES OF EVENTS

 Foreground Events − Those events which require the direct


interaction of a user. They are generated as consequences of
a person interacting with the graphical components in a
Graphical User Interface. For example, clicking on a button,
moving the mouse, entering a character through keyboard,
selecting an item from list, scrolling the page, etc.
 Background Events − Those events that require the
interaction of end user are known as background events. The
operating system interruptions, hardware or software failure,
timer expiry, operation completion are the example of
background events.
EVENTS IN JAVAFX

 Mouse Event
 Key Event
 Drag Event
 Window Event
EVENT HANDLERS AND FILTERS

 Event filters and handlers are those which contains


application logic to process an event.
 A node can register to more than one handler/filter. In case
of parent–child nodes, you can provide a common
filter/handler to the parents, which is processed as default
for all the child nodes.
 All the handlers and filters implement the interface
EventHandler of the package javafx.event.
EVENT HANDLER DEMO

 EventHandling.Java
MENU DEMO

 MenuDemo.Java
JAVA FX: MENUBAR AND MENU

 Menu is a popup menu that contains several menu items that


are displayed when the user clicks a menu. The user can
select a menu item after which the menu goes into a hidden
state.
 MenuBar is usually placed at the top of the screen which
contains several menus. JavaFX MenuBar is typically an
implementation of a menu bar.
CONSTRUCTOR OF THE MENU CLASS ARE:

 Menu(): creates an empty menu


 Menu(String s): creates a menu with a string as its label
 Menu(String s, Node n):Constructs a Menu and sets the
display text with the specified text and sets the graphic Node
to the given node.
 Menu(String s, Node n, MenuItem… i):Constructs a Menu and
sets the display text with the specified text, the graphic Node
to the given node, and inserts the given items into the items
list.

You might also like