0% found this document useful (0 votes)
5 views39 pages

Unit 5

This document provides an overview of JavaFX, a Java library for developing desktop and rich internet applications. It covers event handling, various controls such as CheckBox, ToggleButton, RadioButton, ListView, ComboBox, ChoiceBox, and Text controls, along with their constructors and commonly used methods. Additionally, it includes example programs demonstrating the implementation of these controls in JavaFX applications.

Uploaded by

adchuthan.cs22
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)
5 views39 pages

Unit 5

This document provides an overview of JavaFX, a Java library for developing desktop and rich internet applications. It covers event handling, various controls such as CheckBox, ToggleButton, RadioButton, ListView, ComboBox, ChoiceBox, and Text controls, along with their constructors and commonly used methods. Additionally, it includes example programs demonstrating the implementation of these controls in JavaFX applications.

Uploaded by

adchuthan.cs22
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
You are on page 1/ 39

UNIT-5 CS3391-OOP

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.

1 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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;

public class CHECKBOX extends Application {

public static void main(String[] args) {

launch(args);

2 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

@Override

public void start(Stage primaryStage) throws Exception {

// TODO Auto-generated method stub

Label l = new Label("What do you listen: ");

CheckBox c1 = new CheckBox("Radio one");

CheckBox c2 = new CheckBox("Radio Mirchi");

CheckBox c3 = new CheckBox("Red FM");

CheckBox c4 = new CheckBox("FM GOLD");

HBox root = new HBox();

root.getChildren().addAll(l,c1,c2,c3,c4);

root.setSpacing(5);

Scene scene=new Scene(root,800,200);

primaryStage.setScene(scene);

primaryStage.setTitle("CheckBox Example");

primaryStage.show();

3 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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 {

4 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

primaryStage.setTitle("HBox Experiment 1");


ToggleButton toggleButton1 = new ToggleButton("Left");
ToggleButton toggleButton2 = new ToggleButton("Right");
ToggleButton toggleButton3 = new ToggleButton("Up");
ToggleButton toggleButton4 = new ToggleButton("Down");
ToggleGroup toggleGroup = new ToggleGroup();
toggleButton1.setToggleGroup(toggleGroup);
toggleButton2.setToggleGroup(toggleGroup);
toggleButton3.setToggleGroup(toggleGroup);
toggleButton4.setToggleGroup(toggleGroup);

HBox hbox = new HBox(toggleButton1, toggleButton2, toggleButton3, toggleButton4);


Scene scene = new Scene(hbox, 200, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

OUTPUT

5 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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

6 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

ToggleGroup group = new ToggleGroup();


RadioButton button1 = new RadioButton("option 1");
RadioButton button2 = new RadioButton("option 2");
RadioButton button3 = new RadioButton("option 3");
RadioButton button4 = new RadioButton("option 4");
button1.setToggleGroup(group);
button2.setToggleGroup(group);
button3.setToggleGroup(group);
button4.setToggleGroup(group);
VBox root=new VBox();
root.setSpacing(10);
root.getChildren().addAll(button1,button2,button3,button4);
Scene scene=new Scene(root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Radio Button Example");
primaryStage.show();
}
}

OUTPUT

7 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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();

8 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

}
public static void main(String[] args)
{
launch(args);
}
}

OUTPUT

5.3.5 COMBOBOX

➢ ComboBox is a part of the JavaFX library. JavaFX ComboBox is an implementation


of simple ComboBox which shows a list of items out of which user can select at
most one item, it inherits the class ComboBoxBase.
Constructors of ComboBox:
➢ ComboBox(): creates a default empty combo box
➢ ComboBox(ObservableList i): creates a combo box with the given items
Commonly used Methods:
Method Explanation
getEditor() This method gets the value of the property
editor
getItems() This method returns the items of the combo
box
getVisibleRowCount() This method returns the value of the property
visibleRowCount.

9 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

setItems(ObservableList v) This method Sets the items of the combo box


setVisibleRowCount(int v) This method sets the value of the property
VisibleRowCount

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);
}
}

10 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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.

11 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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

12 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

5.3.7 TEXT CONTROLS


5.3.7.1 JavaFX | TextField
➢ TextField class is a part of JavaFX package. It is a component that allows the user to
enter a line of unformatted text, it does not allow multi-line input it only allows the user
to enter a single line of text. The text can then be used as per requirement.
Constructor of the TextField class:
➢ TextField(): creates a new TextField with empty text content
➢ TextField(String s): creates a new TextField with a initial text .
Commonly used methods:
Method Explanation
setPrefColumnCount(int v) Sets the value of the property
prefColumnCount.
setOnAction(EventHandler value) Sets the value of the property onAction.
setAlignment(Pos v) Sets the value of the property alignment.
prefColumnCountProperty() The preferred number of text columns
onActionProperty() The action handler associated with this text
field, or null if no action handler is assigned.
getPrefColumnCount() Gets the value of the property
prefColumnCount.
getOnAction() Gets the value of the property onAction.
getAlignment() Gets the value of the property alignment.
getCharacters() Returns the character sequence backing the
text field’s content.

5.3.7.2 JavaFX |Password Field


➢ PasswordField class is a part of JavaFX package. It is a Text field that masks entered
characters (the characters that are entered are not shown to the user). It allows the user
to enter a single-line of unformatted text, hence it does not allow multi-line input.
Constructor of the PasswordField class :
PasswordField(): creates a new PasswordField

13 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

Example Program for creating TextField and PasswordField


package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TEXTFIELD extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
TextField tf1=new TextField();
PasswordField pass=new PasswordField();
Button b = new Button("Submit");
VBox root=new VBox();
root.getChildren().addAll(tf1,pass,b);
Scene scene=new Scene(root,300,200);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Field Example");
primaryStage.show();
}
}

14 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

OUTPUT

5.3.7.3 Text Area in JavaFx

➢ 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();
}

15 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

public static void main(String[] args) {


launch(args);
}
}
OUTPUT

5.3.8 SCROLLPANE

➢ ScrollPane is a scrollable component used to display a large content in a limited


space. It contains horizontal and vertical scroll bars.
Syntax for Creating a ScrollPane
ScrollPane scrollPane = new ScrollPane();
ScrollBar Policy
You can set up display policy for scroll bar:
❖ NEVER - Never display
❖ ALWAYS - Always display
❖ AS_NEEDED - Display if needed.
Example Program
package CONTROLS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SCROLLPANE extends Application
{

16 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

@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

17 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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.

18 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

setOrientation(Orientation o) Set the orientation of the pane.


setRowValignment(double v) Sets the value of the property
rowValignment.
setVgap(double v) Sets the vertical gap of the flow pane.
Method Explanation

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);

19 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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

20 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

Methods

Property Description Setter Methods

alignment This represents the alignment of the nodes. setAlignment(Double)

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);

21 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

primaryStage.setWidth(500);
primaryStage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

OUTPUT

22 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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.

Property Description Setter Methods

Alignment This property is for the alignment of the nodes. setAlignement(Double)

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.

23 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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);
}

24 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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

25 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

This class contains five properties, which include


• bottom − This property is of Node type and it represents the node placed at the bottom
of the BorderPane. You can set value to this property using the setter
method setBottom().
• center − This property is of Node type and it represents the node placed at the center
of the BorderPane. You can set value to this property using the setter
method setCenter().
• left − This property is of Node type and it represents the node placed at the left of the
BorderPane. You can set value to this property using the setter method setLeft().
• right − This property is of Node type and it represents the node placed at the right of
the BorderPane. You can set value to this property using the setter method setRight().
• top − This property is of Node type and it represents the node placed at the top of the
BorderPane. You can set value to this property using the setter method setTop().
Example Program
package LAYOUTS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class BORDERPANE 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");
BorderPane root = new BorderPane();
root.setBottom(btn1);
root.setCenter(btn2);
root.setTop(btn3);
Scene scene = new Scene(root);

26 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

primaryStage.setScene(scene);
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
OUTPUT

27 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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.

Property Description Setter Method


alignment It represents the default setAlignment(Node child,
alignment of children within Pos value)
the StackPane's width and
height

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);

28 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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)

29 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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");

30 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

Button btn2 = new Button("2");


Button btn3 = new Button("3");
GridPane root = new GridPane();
root.add(btn1, 0, 0);
root.add(btn2, 1, 1);
root.add(btn3, 2, 2);
Scene scene = new Scene(root);
root.setGridLinesVisible(false);
root.setVgap(10);
root.setHgap(10);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch();
}
}
OUTPUT

31 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

5.5 MENUS, MENU ITEMS AND MENU BAR


➢ Menu is the main component of a any application. 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.
➢ JavaFX provides a Menu class to implement menus.
➢ 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.
➢ 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 MenuBar class are:
➢ MenuBar(): creates a new empty menubar.
➢ MenuBar(Menu… m): creates a new menubar with the given set of menu.
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.
Commonly used methods:
Method Explanation
getItems() returns the items of the menu
hide() hide the menu
show() show the menu
getMenus() The menus to show within this MenuBar.
isUseSystemMenuBar() Gets the value of the property
useSystemMenuBar
setUseSystemMenuBar(boolean v) Sets the value of the property
useSystemMenuBar.
setOnHidden(EventHandler v) Sets the value of the property onHidden.
setOnHiding(EventHandler v) Sets the value of the property onHiding.
setOnShowing(EventHandler v) Sets the value of the property onShowing.

32 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

Example Program for Creating Menus


package Menues;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
//how to create menu in JavaFx
//Let us now create a Menu bar
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);
// 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();

33 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

OUTPUT

34 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

Example Program for Creating Menu Items

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");

35 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

//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);
}
}

36 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

OUTPUT

Example Program for Creating Sub Menus using JavaFx

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)

37 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

{
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);

38 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE


UNIT-5 CS3391-OOP

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

39 PREPARED BY: BASTIN ROGERS C, AP/CSE, SMCE

You might also like