0% found this document useful (0 votes)
0 views

Module_6 (1)

The document provides an introduction to JavaFX, a Java library for building Rich Internet Applications that can run on multiple platforms including desktops and mobile devices. It outlines the installation process for JavaFX in Eclipse, details the architecture, and describes various controls such as Label, Button, and RadioButton. Additionally, it includes examples of basic JavaFX applications and their structure.

Uploaded by

Jayadeep Royal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Module_6 (1)

The document provides an introduction to JavaFX, a Java library for building Rich Internet Applications that can run on multiple platforms including desktops and mobile devices. It outlines the installation process for JavaFX in Eclipse, details the architecture, and describes various controls such as Label, Button, and RadioButton. Additionally, it includes examples of basic JavaFX applications and their structure.

Uploaded by

Jayadeep Royal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button

RadioButton JavaFX C

Object Oriented
Programming
Module 6: Introduction to JavaFX

Dr. Nikhil Mhala

[email protected]
School of Computer Science and Engineering (SCOPE)
VIT-AP University (Besides AP Secretariat) 522237

July 9, 2022

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 1 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Agenda I

6 JavaFX Label
1 JavaFx
7 JavaFX Button
2 Installation of JavaFx
8 RadioButton
3 JavaFX - Architecture 9 JavaFX CheckBox
4 JavaFx Controls & Descrip- 10 JavaFX TextField
tion 11 JavaFX Menu
5 JavaFX Label 12 JavaFX - Event Handling

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 2 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFx

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..

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 3 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX

JavaFX is a Java library that is 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
JavaFX library contains various functionalities like Fundamentals,
2D Shapes, 3D Shapes, Effects, Animation, Text, Layouts, UI
Controls, Transformations, Charts, JavaFX with CSS, JavaFX
with Media etc

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 4 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Installation of JavaFX
Step 1: Ecplise – Install New Software
Open Eclipse and click on Main Menu -> Help -> Install New
Software .

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 5 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Installation of JavaFX
Step 2 : Search for e(fx)clipse
Select the Eclipse Release related site (here Neon) for “Work with”
and enter “e(fx)clipse” in the search bar. Wait for the Eclipse to find
e(fx)clipse.
Check the result e(fx)clispe IDE and click on Next.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 6 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Installation of JavaFX
Step 3 : Review
Review the items and click on Next.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 7 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Installation of JavaFX

Step 4 : Accept License Agreement


Click on the radio button “I accept the terms of the license
agreement” and click on Finish.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 8 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Installation of JavaFX

Step 5 : Wait for the install


Wait for the install to complete and restart the eclipse.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 9 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Installation of JavaFX
Step 6 : Verify the installation
To verify if JavaFX has been installed in Eclipse, Open New Project
window (Main Menu -> New -> Project) Java FX should be listed
under wizards.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 10 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX - Architecture

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 11 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFx Controls & Description

Label
A Label object is a component for placing text.

Button
This class creates a labeled button.

ColorPicker
A ColorPicker provides a pane of controls designed to allow a user to
manipulate and select a color.

CheckBox
A CheckBox is a graphical component that can be in either an
on(true) or off (false) state.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 12 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFx Controls & Description


ListView
A ListView component presents the user with a scrolling list of text
items.

TextField
A TextField object is a text component that allows for the editing of a
single line of text.

PasswordField
A PasswordField object is a text component specialized for password
entry.

Scrollbar
A Scrollbar control represents a scroll bar component in order to
enable user to select from range of values.
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 13 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFx Controls & Description


RadioButton
The RadioButton class is a graphical component, which can either be
in a ON (true) or OFF (false) state in a group.

FileChooser
A FileChooser control represents a dialog window from which the
user can select a file.

ProgressBar
As the task progresses towards completion, the progress bar displays
the task’s percentage of completion.

Slider
A Slider lets the user graphically select a value by sliding a knob
within a bounded interval.
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 14 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

LifeCycle of JavaFx Application

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 15 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Basic Structure of JavaFX

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 16 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Basic Structure of JavaFX


javafx.application.Application
It is the entry point for JavaFX applications
JavaFX creates an application thread for running the application
start method, processing input events, and running animation
timelines
Override the start(Stage) method!

javafx.stage.Stage
It is the top level JavaFX container.

javafx.scene.Scene
class is the container for all content in a scene graph

javafx.scene.Node
It is the base class for scene graph nodes
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 17 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

First JavaFX Program


1 import javafx . application . Application ;
2 import javafx . stage . Stage ;
3 import javafx . scene . Scene ;
4 import javafx . scene . control . Button ;
5 public class MyFirstJavaFX extends Application {
6 @Override // Override the start method in the Application
class
7 public void start ( Stage primaryStage ) {
8 // Create a button and place it in the scene
9 Button btOK = new Button ( " OK " ) ;
10 Scene scene = new Scene ( btOK , 200 , 250) ;
11 primaryStage . setTitle ( " MyJavaFX " ) ; // Set the stage title
12 primaryStage . setScene ( scene ) ; // Place the scene in the
stage
13 primaryStage . show () ; // Display the stage
14 }
15 /* The main method is only needed for the IDE with limited
16 * JavaFX support . Not needed for running from the command
line . */
17 public static void main ( String [] args ) {
18 launch ( args ) ;
19 }
20 } Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 18 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Multiple stages using JavaFX


1 import javafx . application . Application ;
2 import javafx . stage . Stage ;
3 import javafx . scene . Scene ;
4 import javafx . scene . control . Button ;
5 public class M u l t i p l e S t a g e D e m o extends Application {
6 public void start ( Stage primaryStage ) {
7 // Create a scene and place a button in the scene
8 Scene scene = new Scene ( new Button ( " OK " ) , 200 , 250) ;
9 primaryStage . setTitle ( " MyJavaFX " ) ; // Set the stage title
10 primaryStage . setScene ( scene ) ; // Place the scene in the
stage
11 primaryStage . show () ; // Display the stage
12 Stage stage = new Stage () ; // Create a new stage
13 stage . setTitle ( " Second Stage " ) ; // Set the stage title
14 // Set a scene with a button in the stage
15 stage . setScene ( new Scene ( new Button ( " New Stage " ) , 100 , 100) )
;
16 stage . show () ; // Display the stage
17 }
18 public static void main ( String [] args ) {
19 launch ( args ) ;
20 }
21 } Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 19 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Stage:

Represents windows, top level container.


Many setter methods: setTitle(), setWidth().
You can create multiple stages and use one or another
Primary Stage is created internally by the platform. Other stages
can further be created by the application.
Stage.setScene(scene);
scene.show(); makes window appear

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 20 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Scene

Scene
Each stage has a scene.
Javafx.scene.Scene class provides all the methods to deal with a
scene object.
Creating scene is necessary in order to visualize the contents on
the stage.
Scene holds controls (buttons, labels, etc)

Pane
You can put controls in Scenes directly, but we usually Panes for
better layoutExamples: StackPane, BorderPane, HBox, VBox

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 21 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX Label

javafx.scene.control.Label class represents label control. As the


name suggests, the label is the component that is used to place any
text information on the screen. It is mainly used to describe the pur-
pose of the other components to the user.

Constructors
Label(): creates an empty Label
Label(String text): creates Label with the supplied text
Label(String text, Node graphics): creates Label with the sup-
plied text and graphics

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 22 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX Label
1 import javafx . application . Application ;
2 import javafx . scene . Scene ;
3 import javafx . scene . control . Label ;
4 import javafx . scene . layout . StackPane ;
5 import javafx . stage . Stage ;
6 public class LabelTest extends Application {
7 public void start ( Stage primaryStage ) throws Exception {
8 // TODO Auto - generated method stub
9 Label my_label = new Label ( " This is an example of
Label " ) ;
10 StackPane root = new StackPane () ;
11 Scene scene = new Scene ( root ,300 ,300) ;
12 root . getChildren () . add ( my_label ) ;
13 primaryStage . setScene ( scene ) ;
14 primaryStage . setTitle ( " Label Class Example " ) ;
15 primaryStage . show () ;
16 }
17 public static void main ( String [] args ) {
18 launch ( args ) ;
19 }
20 }

./code/LabelTest.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 23 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX Button

JavaFX button control is represented by javafx.scene.control.Button


class. A button is a component that can control the behaviour of the
Application. An event is generated whenever the button gets clicked.

How to create Button?


Button btn = new Button("My Button");

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 24 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example Adding button to the scene graph


1 import javafx . application . Application ;
2 import javafx . scene . Scene ;
3 import javafx . scene . control . Button ;
4 import javafx . scene . layout . StackPane ;
5 import javafx . stage . Stage ;
6 public class ButtonTest extends Application {
7 @Override
8 public void start ( Stage primaryStage ) throws Exception {
9 StackPane root = new StackPane () ;
10 Button btn = new Button ( " This is a button " ) ;
11 Scene scene = new Scene ( root ,300 ,300) ;
12 root . getChildren () . add ( btn ) ;
13 primaryStage . setScene ( scene ) ;
14 primaryStage . setTitle ( " Button Class Example " ) ;
15 primaryStage . show () ;
16 }
17 public static void main ( String [] args ) {
18 launch ( args ) ;
19 }
20 }

./code/ButtonTest.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 25 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

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 is either selected or deselected.
It can be used in a scenario of multiple choice questions in the
quiz where only one option needs to be chosen by the student.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 26 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example; creating a Radio Button

1 import javafx . application . Application ;


2 import javafx . scene . Scene ;
3 import javafx . scene . control . RadioButton ;
4 import javafx . scene . control . ToggleGroup ;
5 import javafx . scene . layout . VBox ;
6 import javafx . stage . Stage ;
7 public class R ad io B ut to n Te s t extends Application {
8 public static void main ( String [] args ) {
9 launch ( args ) ;
10 }
11 @Override
12 public void start ( Stage primaryStage ) throws Exception {
13 ToggleGroup group = new ToggleGroup () ;
14 RadioButton button1 = new RadioButton ( " option 1 " ) ;
15 RadioButton button2 = new RadioButton ( " option 2 " ) ;
16 RadioButton button3 = new RadioButton ( " option 3 " ) ;
17 RadioButton button4 = new RadioButton ( " option 4 " ) ;

./code/RadioButtonTest.java

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 27 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example; creating a Radio Button

19 button1 . se tToggl eGroup ( group ) ;


20 button2 . se tToggl eGroup ( group ) ;
21 button3 . se tToggl eGroup ( group ) ;
22 button4 . se tToggl eGroup ( group ) ;
23 VBox root = new VBox () ;
24 root . setSpacing (10) ;
25 root . getChildren () . addAll ( button1 , button2 , button3 ,
button4 ) ;
26 Scene scene = new Scene ( root ,400 ,300) ;
27 primaryStage . setScene ( scene ) ;
28 primaryStage . setTitle ( " Radio Button Example " ) ;
29 primaryStage . show () ;
30 }
31 }

./code/RadioButtonTest.java

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 28 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX CheckBox

The Check Box is used to provide more than one choices to the
user.
It can be used in a scenario where the user is prompted to select
more than one option or the user wants to select multiple
options.
Instantiate javafx.scene.control.CheckBox class to implement
CheckBox

1 CheckBox checkbox = new CheckBox () ;


2 CheckBox checkbox = new CheckBox ( " Label Name " ) ;

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 29 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example: JavaFX CheckBox


1 import javafx . application . Application ;
2 import javafx . scene . Scene ;
3 import javafx . scene . control . CheckBox ;
4 import javafx . scene . control . Label ;
5 import javafx . scene . layout . HBox ;
6 import javafx . stage . Stage ;
7
8 public class CheckBoxTest extends Application {
9 public static void main ( String [] args ) {
10 launch ( args ) ;
11 }
12
13 @Override
14 public void start ( Stage primaryStage ) throws Exception {
15 // TODO Auto - generated method stub
16 Label l = new Label ( " What do you listen : ");
17 CheckBox c1 = new CheckBox ( " Radio one " ) ;
18 CheckBox c2 = new CheckBox ( " Radio Mirchi " ) ;
19 CheckBox c3 = new CheckBox ( " Red FM " ) ;
20 CheckBox c4 = new CheckBox ( " FM GOLD " ) ;

./code/CheckBoxTest.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 30 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example: JavaFX CheckBox

21
22 HBox root = new HBox () ;
23 root . getChildren () . addAll (l , c1 , c2 , c3 , c4 ) ;
24 root . setSpacing (5) ;
25 Scene scene = new Scene ( root ,800 ,200) ;
26 primaryStage . setScene ( scene ) ;
27 primaryStage . setTitle ( " CheckBox Example " ) ;
28 primaryStage . show () ;
29 }
30 }

./code/CheckBoxTest.java

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 31 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX TextField

Text Field is basically used to get the input from the user in the
form of text.
javafx.scene.control.TextField represents TextField.
It provides various methods to deal with textfields in JavaFX.
TextField can be created by instantiating TextField class

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 32 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example JavaFX TextField


1 import javafx . application . Application ;
2 import javafx . scene . Scene ;
3 import javafx . scene . control . Button ;
4 import javafx . scene . control . Label ;
5 import javafx . scene . control . TextField ;
6 import javafx . scene . layout . GridPane ;
7 import javafx . stage . Stage ;
8 public class TextFieldTest extends Application {
9
10
11 public static void main ( String [] args ) {
12 launch ( args ) ;
13 }
14
15 @Override
16 public void start ( Stage primaryStage ) throws Exception {
17 // TODO Auto - generated method stub
18 Label user_id = new Label ( " User ID " ) ;
19 Label password = new Label ( " Password " ) ;

./code/TextFieldTest.java

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 33 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example JavaFX TextField

21 TextField tf1 = new TextField () ;


22 TextField tf2 = new TextField () ;
23 Button b = new Button ( " Submit " ) ;
24 GridPane root = new GridPane () ;
25 root . addRow (0 , user_id , tf1 ) ;
26 root . addRow (1 , password , tf2 ) ;
27 root . addRow (2 , b ) ;
28 Scene scene = new Scene ( root ,800 ,200) ;
29 primaryStage . setScene ( scene ) ;
30 primaryStage . setTitle ( " Text Field Example " ) ;
31 primaryStage . show () ;
32 }
33 }

./code/TextFieldTest.java

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 34 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX Menu

JavaFX provides a Menu class to implement menus.


Menu is the main component of a any application.
In JavaFX, javafx.scene.control.Menu class provides all the
methods to deal with menus.
This class needs to be instantiated to create a Menu.

1 ManuBar menubar = new MenuBar () ; // creating MenuBar


2 Menu MenuName = new Menu ( " Menu Name " ) ; // creating Menu
3 MenuItem MenuItem1 = new MenuItem ( " Menu Item 1 Name " ) ; //
creating Menu Item
4 MenuName . getItems () . add ( MenuItem1 ) ; // adding Menu Item to
the Menu
5 menubar . getMenus () . add ( MenuName ) ; // adding Menu to the
MenuBar

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 35 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example: JavaFX Menu


1 import javafx . application . Application ;
2 import javafx . scene . Scene ;
3 import javafx . scene . control .*;
4 import javafx . scene . layout . BorderPane ;
5 import javafx . stage . Stage ;
6 public class MenuExample extends Application {
7 public static void main ( String [] args ) {
8 launch ( args ) ;
9 }
10 @Override
11 public void start ( Stage primaryStage ) throws Exception {
12 // TODO Auto - generated method stub
13 BorderPane root = new BorderPane () ;
14 Scene scene = new Scene ( root ,200 ,300) ;
15 MenuBar menubar = new MenuBar () ;
16 Menu FileMenu = new Menu ( " File " ) ;
17
18 MenuItem filemenu1 = new MenuItem ( " new " ) ;
19 MenuItem filemenu2 = new MenuItem ( " Save " ) ;
20 MenuItem filemenu3 = new MenuItem ( " Exit " ) ;

./code/MenuExample.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 36 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Example: JavaFX Menu

21
22 Menu EditMenu = new Menu ( " Edit " ) ;
23 MenuItem EditMenu1 = new MenuItem ( " Cut " ) ;
24 MenuItem EditMenu2 = new MenuItem ( " Copy " ) ;
25 MenuItem EditMenu3 = new MenuItem ( " Paste " ) ;
26 EditMenu . getItems () . addAll ( EditMenu1 , EditMenu2 ,
EditMenu3 ) ;
27 root . setTop ( menubar ) ;
28 FileMenu . getItems () . addAll ( filemenu1 , filemenu2 ,
filemenu3 ) ;
29 menubar . getMenus () . addAll ( FileMenu , EditMenu ) ;
30 primaryStage . setScene ( scene ) ;
31 primaryStage . show () ;
32
33 }
34 }

./code/MenuExample.java

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 37 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

JavaFX - Event Handling

In applications, whenever a user interacts with the application


(nodes), an event is said to have been occurred

Types of Event
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 don’t 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.

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 38 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Events in JavaFX
The class named Event of the package javafx.event is the base
class for an event
Mouse Event This is an input event that occurs when a mouse
is clicked. It is represented by the class named MouseEvent. It
includes actions like mouse clicked, mouse pressed, mouse
released, mouse moved, mouse entered target, mouse exited
target, etc.
Key Event This is an input event that indicates the key stroke
occurred on a node. It is represented by the class named
KeyEvent. This event includes actions like key pressed, key
released and key typed.
Drag Event This is an input event which occurs when the
mouse is dragged. It is represented by the class named
DragEvent. It includes actions like drag entered, drag dropped,
drag entered target, drag exited target, drag over, etc.
Window Event This is an event related to window
showing/hiding actions. It is represented by the class named
WindowEvent. It includes actions like window hiding, window
shown,
Dr. Nikhil window hidden, window
Mhala VIT-AP showing, etc.
CSE2005 July 9, 2022 39 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Event Handling

Event Handling is the mechanism that controls the event and


decides what should happen, if an event occurs. This mecha-
nism has the code which is known as an event handler that is
executed when an event occurs.
JavaFX provides handlers and filters to handle events. In JavaFX
every event has
Target The node on which an event occurred. A target can be a
window, scene, and a node.
Source The source from which the event is generated will be the
source of the event. In the above scenario, mouse is the source
of the event.
Type Type of the occurred event; in case of mouse event –
mouse pressed, mouse released are the type of events

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 40 / 45


JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Event Handling Example


1 import javafx . application . Application ;
2 import static javafx . application . Application . launch ;
3 import javafx . event . EventHandler ;
4
5 import javafx . scene . Group ;
6 import javafx . scene . Scene ;
7 import javafx . scene . input . MouseEvent ;
8 import javafx . scene . paint . Color ;
9 import javafx . scene . shape . Circle ;
10
11 import javafx . scene . text . Font ;
12 import javafx . scene . text . FontWeight ;
13 import javafx . scene . text . Text ;
14 import javafx . stage . Stage ;
15
16 public class E v e n t F i l t e r s E x a m p l e extends Application {
17 @Override
18 public void start ( Stage stage ) {
19 // Drawing a Circle
20 Circle circle = new Circle () ;

./code/EventFiltersExample.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 41 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Event Handling Example


21
22 // Setting the position of the circle
23 circle . setCenterX (300.0 f ) ;
24 circle . setCenterY (135.0 f ) ;
25
26 // Setting the radius of the circle
27 circle . setRadius (25.0 f ) ;
28
29 // Setting the color of the circle
30 circle . setFill ( Color . BROWN ) ;
31
32 // Setting the stroke width of the circle
33 circle . setSt rokeWi dth (20) ;
34
35 // Setting the text
36 Text text = new Text ( " Click on the circle to change
its color " ) ;
37
38 // Setting the font of the text
39 text . setFont ( Font . font ( null , FontWeight . BOLD , 15) ) ;

./code/EventFiltersExample.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 42 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Event Handling Example


41 // Setting the color of the text
42 text . setFill ( Color . CRIMSON ) ;
43
44 // setting the position of the text
45 text . setX (150) ;
46 text . setY (50) ;
47
48 // Creating the mouse event handler
49 EventHandler < MouseEvent > eventHandler = new
EventHandler < MouseEvent >() {
50 @Override
51 public void handle ( MouseEvent e ) {
52 System . out . println ( " Hello World " ) ;
53 circle . setFill ( Color . DARKSLATEBLUE ) ;
54 }
55 };
56 // Registering the event filter
57 circle . addEv entFil ter ( MouseEvent . MOUSE_CLICKED ,
eventHandler ) ;
58
59 // Creating a Group object
60 Group root = new Group ( circle , text ) ;
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 43 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Event Handling Example


61
62 // Creating a scene object
63 Scene scene = new Scene ( root , 600 , 300) ;
64
65 // Setting the fill color to the scene
66 scene . setFill ( Color . LAVENDER ) ;
67
68 // Setting title to the Stage
69 stage . setTitle ( " Event Filters Example " ) ;
70
71 // Adding scene to the stage
72 stage . setScene ( scene ) ;
73
74 // Displaying the contents of the stage
75 stage . show () ;
76 }
77 public static void main ( String args []) {
78 launch ( args ) ;
79 }
80 }

./code/EventFiltersExample.java
Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 44 / 45
JavaFx Installation of JavaFx JavaFX - Architecture JavaFx Controls & Description JavaFX Label JavaFX Label JavaFX Button RadioButton JavaFX C

Dr. Nikhil Mhala

Contact:
[email protected]
Landline: 0863-2370961 (Mon-Fri 9 am - 5 pm)
Extension: 5961

Dr. Nikhil Mhala VIT-AP CSE2005 July 9, 2022 45 / 45

You might also like