Unit 5
Unit 5
UNIT V
JAVAFX EVENT HANDLING, CONTROLS AND
COMPONENTS
JAVAFX Events and Controls: Event Basics – Handling Key and Mouse
Events. Controls: Checkbox, ToggleButton – RadioButtons – ListView –
ComboBox – ChoiceBox – Text Controls – ScrollPane. Layouts –
FlowPane – HBox and VBox – BorderPane – StackPane – GridPane.
Menus – Basics – Menu – Menu bars – MenuItem.
What is JavaFX?
➢ 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 announces 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.
5.3 JavaFX Controls
5.3.1 CHECKBOX
➢ The Check Box is used to provide more than one choice 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.
➢ It is different from the radiobutton in the sense that, we can select more than one
checkboxes in a scenerio.
➢ Instantiate javafx.scene.control.CheckBox class to implement CheckBox.
States of CheckBox:
➢ Checked: When indeterminate is false and checked is true
➢ Unchecked: When indeterminate is false and checked is false
➢ Undefined: When indeterminate is true
Constructor of the class are:
CheckBox() : Creates a check box with an empty string for its label.
CheckBox(String t) : Creates a check box with the given text as its label.
Commonly used methods:
Method Explanation
isIndeterminate() Gets the value of the property indeterminate.
isSelected() Gets the value of the property selected.
selectedProperty() Indicates whether this CheckBox is checked.
setIndeterminate(boolean v) Sets the value of the property indeterminate.
setSelected(boolean v) Sets the value of the property selected.
Example Program
package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
launch(args);
@Override
root.getChildren().addAll(l,c1,c2,c3,c4);
root.setSpacing(5);
primaryStage.setScene(scene);
primaryStage.setTitle("CheckBox Example");
primaryStage.show();
OUTPUT
5.3.2 TOGGLEBUTTON
➢ A JavaFX ToggleButton is a button that can be selected or not selected. Like a button
that stays in when you press it, and when you press it the next time it comes out again.
Toggled - not toggled.
➢ The JavaFX ToggleButton is represented by the class
javafx.scene.control.ToggleButton .
Creating a ToggleButton
➢ You create a JavaFX ToggleButton by creating an instance of the ToggleButton class.
Here is an example of creating a JavaFX ToggleButton instance:
ToggleButton toggleButton1 = new ToggleButton("Left");
➢ This example creates a ToggleButton with the text Left on.
Example Program
package controls;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ToggleButtonExperiments extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
OUTPUT
5.3.3 RADIOBUTTONS
➢ 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.
Constructors of the RadioButton class:
1. RadioButton():Creates a radio button with an empty string for its label.
2. RadioButton(String t):Creates a radio button with the specified text as its label
Commonly used methods:
Method Explanation
getText() returns the textLabel for radio button
isSelected() returns whether the radiobutton is selected or
not
setSelected(boolean b) sets whether the radiobutton is selected or not
setToggleGroup(ToggleGroup tg) sets the toggle group for the radio button
Example Program
package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RADIOBUTTON extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
OUTPUT
5.3.4 LISTVIEW
➢ A list view is a scrollable list of items from which you can select desired items. You
can create a list view component by instantiating the javafx.scene.control.ListView
class. You can create either a vertical or a horizontal ListView.
Syntax:
Creating a ListView
ListView listView = new ListView();
Adding Items to a ListView
listView.getItems().add("Item 1");
listView.getItems().add("Item 2");
listView.getItems().add("Item 3");
Example Program
package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class LISTVIEW extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ListView Experiment 1");
ListView<String> listView = new ListView<String>();
listView.getItems().add("Item 1");
listView.getItems().add("Item 2");
listView.getItems().add("Item 3");
HBox hbox = new HBox(listView);
Scene scene = new Scene(hbox, 300, 120);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
OUTPUT
5.3.5 COMBOBOX
Example Program
package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class COMBOBOX extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ComboBox Experiment 1");
ComboBox<String> comboBox = new ComboBox<String>();
comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");
HBox hbox = new HBox(comboBox);
Scene scene = new Scene(hbox, 200, 120);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT
5.3.6 CHOICEBOX
➢ ChoiceBox is a part of the JavaFX package. ChoiceBox shows a set of items and allows
the user to select a single choice and it will show the currently selected item on the top.
ChoiceBox by default has no selected item unless otherwise selected.
Constructor of the ChoiceBox class are:
➢ ChoiceBox(): Creates a new empty ChoiceBox.
➢ ChoiceBox(ObservableList items): Creates a new ChoiceBox with the given set of
items.
Commonly used methods:
Method Explanation
getItems() Gets the value of the property items.
getValue() Gets the value of the property value.
hide() Closes the list of choices.
setItems(ObservableList value) Sets the value of the property items.
setValue(T value) Sets the value of the property value.
show() Opens the list of choices.
Example Program
package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class CHOICEBOX extends Application
{
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("ChoiceBox Experiment 1");
ChoiceBox<String> choiceBox = new ChoiceBox<String>();
choiceBox.getItems().add("Choice 1");
choiceBox.getItems().add("Choice 2");
choiceBox.getItems().add("Choice 3");
HBox hbox = new HBox(choiceBox);
Scene scene = new Scene(hbox, 200, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT
OUTPUT
➢ A text area is a multi-line editor where you can enter text. Unlike previous versions, in
the latest versions of JavaFX, a TextArea does not allow single lines in it. You can
create a text area by instantiating the javafx.scene.control.TextArea class.
Example Program
package controls;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TEXTAREA extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("TextArea Experiment 1");
TextArea textArea = new TextArea();
VBox vbox = new VBox(textArea);
Scene scene = new Scene(vbox, 200, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
5.3.8 SCROLLPANE
@Override
public void start(Stage primaryStage)
{
// setting the title of application
primaryStage.setTitle("ScrollPane Vertical");
// Create a ScrollPane
ScrollPane scrollPane = new ScrollPane();
VBox vBox=new VBox();
// Setting the content to the ScrollPane
scrollPane.setContent(vBox);
// Always show vertical scroll bar for scrolling
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
//adding scroll pane to the scene
Scene scene = new Scene(scrollPane,200,300);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args)
{
//invoking main method from JVM
launch(args);
}
}
OUTPUT
5.4 LAYOUTS
5.4.1 FlowPane
➢ FlowPane class is a part of JavaFX. Flowpane lays out its children in such a way
that wraps at the flowpane’s boundary.
➢ A horizontal flowpane (the default) will layout nodes in rows, wrapping at the
flowpane’s width.
➢ A vertical flowpane lays out nodes in columns, wrapping at the flowpane’s height.
FlowPane class inherits Pane class.
Constructors of the class:
❖ FlowPane(): Creates a new Horizontal FlowPane layout.
❖ FlowPane(double h, double v): Creates a new Horizontal FlowPane layout, with
specified horizontal and vertical gap.
❖ FlowPane(double h, double v, Node… c): Creates a new Horizontal FlowPane layout,
with specified horizontal, vertical gap and nodes.
❖ FlowPane(Node… c): Creates a FlowPane with specified childrens.
❖ FlowPane(Orientation o): Creates a FlowPane with specified orientation
❖ FlowPane(Orientation o, double h, double v): Creates a FlowPane with specified
orientation and specified horizontal and vertical gap.
❖ FlowPane(Orientation o, double h, double v, Node… c): Creates a FlowPane with
specified orientation and specified horizontal and vertical gap and specified childrens.
❖ FlowPane(Orientation o, Node… c): Creates a FlowPane with specified orientation and
specified nodes.
Commonly Used Methods:
Method Explanation
getAlignment() Returns the value of Alignment of the pane.
getHgap() Returns the horizontal gap of the flow pane.
getOrientation() Returns the orientation of the pane.
getRowValignment() Gets the value of the property
rowValignment.
getVgap() Returns the vertical gap of the flow pane.
setAlignment(Pos v) Set the value of Alignment of the pane.
setHgap(double v) Sets the horizontal gap of the flow pane.
Example Program
package LAYOUTS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class FLOWPANE extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Button btn1 = new Button("1");
Button btn2 = new Button("2");
Button btn3 = new Button("3");
Button btn4 = new Button("4");
Button btn5 = new Button("5");
Button btn6 = new Button("6");
Button btn7 = new Button("7");
Button btn8 = new Button("8");
Button btn9 = new Button("9");
Button btn10 = new Button("10");
Button btn11 = new Button("11");
Button btn12 = new Button("12");
FlowPane root = new FlowPane();
Scene scene = new Scene(root,100,100);
root.getChildren().addAll(btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12);
root.setVgap(6);
root.setHgap(5);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
OUTPUT
5.4.2 HBox
➢ The JavaFX HBox component is a layout component which positions all its child
nodes(components) in a horizontal row. It is represented by
javafx.scene.layout.HBox class. We just need to instantiate HBox class in order to
create HBox layout.
Constructors:
❖ new HBox() : create HBox layout with 0 spacing
❖ new Hbox(Double spacing) : create HBox layout with a spacing value
Methods
fillHeight This is a boolean property. If you set this property to true setFillHeight(Double)
the height of the nodes will become equal to the height of
the HBox.
spacing This represents the space between the nodes in the HBox. setSpacing(Double)
It is of double type.
Example Program
package LAYOUTS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class HORIZONTAL extends Application {
@Override
public void start(Stage primaryStage) {
try
{
Button btn1 = new Button("1");
Button btn2 = new Button("2");
Button btn3 = new Button("3");
Button btn4 = new Button("4");
HBox root = new HBox();
Scene scene = new Scene(root,200,200);
root.getChildren().addAll(btn1,btn2,btn3,btn4);
root.setSpacing(40);
primaryStage.setScene(scene);
primaryStage.setHeight(800);
primaryStage.setWidth(500);
primaryStage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT
5.4.3 VBox
➢ Instead of arranging the nodes in horizontal row, Vbox Layout Pane arranges the
nodes in a single vertical column.
➢ It is represented by javafx.scene.layout.VBox class which provides all the methods
to deal with the styling and the distance among the nodes. This class needs to be
instantiated in order to implement VBox layout in our application.
Constructors
❖ VBox() : creates layout with 0 spacing
❖ Vbox(Double spacing) : creates layout with a spacing value of double type
❖ Vbox(Double spacing, Node? children) : creates a layout with the specified spacing
among the specified child nodes
❖ Vbox(Node? children) : creates a layout with the specified nodes having 0 spacing
among them
Methods
This Method Provides various properties which are described in the table below.
FillWidth This property is of the boolean type. The Widtht of resizeable setFillWidth(boolean)
nodes can be made equal to the Width of the VBox by setting
this property to true.
Spacing This property is to set some spacing among the nodes of setSpacing(Double)
VBox.
Example Program
package LAYOUTS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class VERTICAL extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Button btn1 = new Button("1");
Button btn2 = new Button("2");
Button btn3 = new Button("3");
Button btn4 = new Button("4");
Button btn5 = new Button("5");
Button btn6 = new Button("6");
Button btn7 = new Button("7");
Button btn8 = new Button("8");
Button btn9 = new Button("9");
VBox root = new VBox();
root.setSpacing(20);
Scene scene = new Scene(root);
root.getChildren().addAll(btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9);
root.setSpacing(40);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
OUTPUT
5.4.4 BorderPane
➢ If we use the BorderPane, the nodes are arranged in the Top, Left, Right, Bottom
and Center positions.
➢ The class named BorderPane of the package javafx.scene.layout represents the
BorderPane.
primaryStage.setScene(scene);
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
OUTPUT
5.4.5 StackPane
➢ The StackPane layout pane places all the nodes into a single stack where every new
node gets placed on the top of the previous node. It is represented by
javafx.scene.layout.StackPane class. We just need to instantiate this class to implement
StackPane layout into our application.
Properties
The class contains only one property that is given below along with its setter method.
Constructors
The class contains two constructors that are given below.
❖ StackPane()
❖ StackPane(Node? Children)
Example Program
package LAYOUTS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class STACKPANE extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1 on bottom ");
Button btn2 = new Button("Button 2 on top");
StackPane root = new StackPane();
Scene scene = new Scene(root,200,200);
root.getChildren().addAll(btn1,btn2);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT
5.4.6 GridPane
➢ GridPane Layout pane allows us to add the multiple nodes in multiple rows and
columns. It is seen as a flexible grid of rows and columns where nodes can be placed
in any cell of the grid.
➢ It is represented by javafx.scence.layout.GridPane class. We just need to instantiate this
class to implement GridPane.
Following are the cell positions in the grid pane of JavaFX −
(0, 0) (1, 0) (2, 0)
(2, 1) (1, 1) (0, 1)
(2, 2) (1, 2) (0, 2)
Properties
➢ The properties of the class along with their setter methods are given in the table below.
Property Description Setter Methods
alignment Represents the setAlignment(Pos value)
alignment of the grid
within the GridPane.
gridLinesVisible This property is intended setGridLinesVisible(Boolean
for debugging. Lines can value)
be displayed to show the
gridpane's rows and
columns by setting this
property to true.
hgap Horizontal gaps among setHgap(Double value)
the columns
vgap Vertical gaps among the setVgap(Double value)
rows
Constructors
❖ The class contains only one constructor that is given below.
Public GridPane(): creates a gridpane with 0 hgap/vgap
Example Program
package LAYOUTS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class GRIDPANE extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Button btn1 = new Button("1");
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
OUTPUT
package Menues;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MENUITEMS extends Application
{
@Override
public void start(Stage primaryStage)
{
try
{
MenuBar main_menu=new MenuBar();
Menu File=new Menu("File");
Menu Edit=new Menu("Edit");
Menu Source=new Menu("Source");
Menu Refactor=new Menu("Refactor");
// Mapping all the menu objects to menu bar
main_menu.getMenus().add(File);
main_menu.getMenus().add(Edit);
main_menu.getMenus().add(Source);
main_menu.getMenus().add(Refactor);
//Let us add Menu Items for File Menu
MenuItem New=new MenuItem("New");
MenuItem OpenFile=new MenuItem("Open File...");
MenuItem OpenProjects=new MenuItem("Open Projects From File
Systems...");
MenuItem RecentFiles=new MenuItem("Recent Files");
MenuItem Save=new MenuItem("Save");
OUTPUT
package Menues;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class SUBMENU extends Application
{
@Override
public void start(Stage primaryStage)
{
try
{
MenuBar main_menu=new MenuBar();
Menu File=new Menu("File");
Menu Edit=new Menu("Edit");
Menu Source=new Menu("Source");
Menu Refactor=new Menu("Refactor");
// Mapping all the menu objects to menu bar
main_menu.getMenus().add(File);
main_menu.getMenus().add(Edit);
main_menu.getMenus().add(Source);
main_menu.getMenus().add(Refactor);
//Let us add Menu Items for File Menu
Menu New=new Menu("New"); //New is not a menu item its a menu
MenuItem OpenFile=new MenuItem("Open File...");
MenuItem OpenProjects=new MenuItem("Open Projects From File Systems...");
MenuItem RecentFiles=new MenuItem("Recent Files");
MenuItem Save=new MenuItem("Save");
//We will create Menu Items for New Menu
MenuItem JavaProject=new MenuItem("Java Project");
MenuItem Project=new MenuItem("Project");
MenuItem Package1=new MenuItem("Package");
MenuItem Class1=new MenuItem("Class");
//Mapping Menu Items to Menu New
New.getItems().add(JavaProject);
New.getItems().add(Project);
New.getItems().add(Package1);
New.getItems().add(Class1);
//Map the Menu Items to the File Menu
File.getItems().add(New);
File.getItems().add(OpenFile);
File.getItems().add(OpenProjects);
File.getItems().add(RecentFiles);
File.getItems().add(Save);
// Create a Layout and add the menu bar to the Layout
BorderPane root=new BorderPane();
root.setTop(main_menu);
//we need to add this Layout to the Scene
Scene sc=new Scene(root);
primaryStage.setScene(sc);
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
OUTPUT