Unit 5
Unit 5
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.
Basicas of JAVAFX
• JavaFX was originally developed by Chris Oliver, when he was working for a company
named See Beyond Technology Corporation, which was later acquired by Sun
Microsystems in the year 2005.
• 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 different
platforms.
Games in Java.
• JavaFX has replaced Swing as the recommended GUI toolkit for Java. Furthermore,
JavaFX is more consistent in its design than Swing, and has more features.
Features of JAVAFX
1) JavaFX library is written in java. Hence developer find it easy to learn and write the
applications in JAVAFX.
2) JAVAFX library creates UI controls using which any GUI based application can be
developed.
4) JavaFX contains a set of ready-to-use chart components, so you don't have to code that
from scratch every time you need a basic chart.
Every JAVAFX program is divided into three main components - Stage, Scene and Node
and Scene graph.
(1) Stage; Stage is like main frame of the program. It acts as a container for all the objects
of JavaFX application. The most commonly used stage is a PrimaryStage. It is created
internally by the platform. The object of PrimaryStage is passed to the start() method. There
is a show method that is used to show the stage.
(2) Scene: The Javafx.scene.Scene class provides the methods to deal with the scene object.
It contains the nodes or all the contents of Scene graph. The scene is created by instantiating
the Scene class.
(3) Scene Graph and Node: The scene graph is a collection of various nodes. The node is
an element that can be visualized on the screen. The node can be button, textbox, radio
button and so on.
The nodes are implemented in tree like manner. There is one root node. This will be the
parent node of all the other nodes.
(1) start(): This is the method in which we write the code for JavaFX application. The
construct for this method is
(2) launch() 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. Since the
launch() method is static, you need to call it from a static method such as main()
Prerequisite: Before executing the JavaFX application program, you need to have following
things installed in your PC
i) Java 8
ii) NetBeans 8
Step 1: From the File menu, choose New Project. Then select JavaFX application category,
choose JavaFX Application. Click Next.
Then the code can be written as follows
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
@Override
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Step 2: Now right click on the source file and click on Run File As. The output will be
displayed as follows
Output
Program Explanation: In above program,
2. Then we need to import the classes for supporting - Stage Scene, Label component and
launch method. Hence we add following lines in our code.
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
4. Then we write overridden start() method in which the entire application code is written.
The primaryStage is passed as a framework to this start method as a parameter. Note that
these are all standard steps for any JavaFX application program.
5. Then we have -
Step 3: Using setScene method we have set this scene for Stage
primaryStage.setScene(scene);
Step 4: If you want to display the title for your application page, then it is possible by using
setTitle method for the Stage component. Keep it in mind that here stage is something like
a frame on which scene, UI components are placed.
Step 5: Finally any component can be made visible by using show() method.
primaryStage.show();
6. Then write the main() method which in turn will call launch() method to call your start()
method.
launch(args);
That's it. Now run your application program and a Label component with some message
will be displayed as an output.
• Generally you place a node inside a pane and then place pane into a scene. Actually node
is any visual component such as UI controls, shapes, or a image view.
• A Scene can be displayed in a Stage. The relationship between node, Pane, Scene, Stage
is shown by following figure.
• Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
root.getChildren().add(btn);
primaryStage.setTitle("Demo Program");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
Program Explanation: In above program,
1) We have created a Pane named StackPane, and added a button component as a child of
the pane. For that purpose we use getChildren() method.
2) The StackPane places the nodes in t he center of the pane on top of each other. Here,
there is only one node in the pane. The use of Pane allows the button component to be
displayed with its preferred size.
Property Binding
• JavaFX introduces a new concept called binding property that enables a target object to be
bound to a source object.
• If the value in the source object changes, the target property is also changed automatically.
• A target listens to the changes in the source and automatically updates itself once a change
is made in the source. A target binds with a source using the bind method as follows:
target.bind(source);
• Event means any activity that interrupts the current ongoing activity. For example: when
user clicks button then it generates an event. To respond to button click we need to write the
code to process the button clicking action.
• Event Source Object : The object that generates the event is called event source object.
For example- if the event gets generated on clicking the button, then button is the event
source object.
• Event Handler: The event handling code written to process the generated event is called
event handler.
• Event Listener: The task of handling an event is carried out by event listener. When an
event occurs, first of all an event object of the appropriate type is created. This object is
then passed to a Listener. A listener must implement the interface that has the method for
event handling.
• JavaFX has just one interface for all kinds of Event Handlers.
• The EventHandler object handler must be registered with the event source object using the
method source.setOnAction(handler).
• There are user actions that are associated with some source objects. For these actions
particular event is fired. The JavaFX associate some setOnXXX methods for registering
these events. The list of such user actions, source objects and setOnXXX methods are
enlisted in the following table.
• Java makes use of delegation based model for event handling and registration. This model
is as shown below.
The event handling process is a two step process. It performs following functionalities –
(2) The handler object is registered by the source object. The registration methods depend
on the event type. For ActionEvent, the method is setOnAction. For a mouse pressed event,
the method is setOnMousePressed. For a key pressed event, the method is
setOnKeyPressed.
For example
………
……...
Following is a complete JavaFX program, that displays the message on clicking the button.
Thus button click event is handled in the following program.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
@Override
System.out.println("Button is clicked!!");
};
root.getChildren().add(button);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
Output
On clicking the above button, we get following message in the output window
Mouse event is fired when the mouse button is pressed, released, clicked, moved or dragged
on the node.
Following table shows the user actions and associated event handling activities -
Various mehods for MouseEvent class are enlisted in the following table
There are Four constants-PRIMARY, SECONDARY, MIDDLE, and NONE - are defined
in MouseButton to indicate the left, right, middle, and none mouse buttons.
Programming Example
package myjavafxapplication;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
@Override
Rectangle(50,50,100,100); rect.setArcWidth(20);
rect.setArcHeight(20);
rect.setFill(Color.BLUE);
Text text = new Text (20,20,"Click on the rectangle to change its color");
rect.setOnMouseClicked(e-
>{ rect.setFill(Color.RED);
});
Group root = new Group (rect,text);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
(1) We have created one rectangular object and it is colored using blue color.
(2) The mouse click event is used. When user clicks over the rectangle, the color of the
rectangle gets changed to red.
rect.setOnMouseClicked(e->{
rect.setFill(Color.RED);
});
Keyboard Events
When any key on the keyboard is pressed, released, or typed on a node then the keyboard
event occurs.
The associated keyboard event triggering actions made by user, type of event and event
registration methods are enlisted in the following table.
For recognizing the keyboard events we use KeyEvent class. Various methods of KeyEvent
class are as described in the following table.
The key codes returned by the getCode() method are constants. These constants are enlisted
in the following table -
Following is a simple keyboard event handling program that displays the character typed on
the output window.
Programming Example
package myjavafxapplication;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
@Override
text.setFont(Font.font("Arial", FontWeight.BOLD,30));
text.setOnKeyPressed(e-
>{ text.setText(e.getText());
});
primaryStage.setScene(scene);
primaryStage.show();
text.requestFocus();
launch(args);
}
Output
(1) For displaying the character typed we have set the Text node. As we want bold,arial and
a
(2) Then the keybpard event is set using the lambda function
text.setOnKeyPressed(e->{
text.setText(e.getText());
});
Controls
• The graphical User Interface (GUI) is an essential part of any window based application.
• The interaction between user and the application program is possible with the help of User
Interface (UI) controls that are placed on the screen.
• JavaFX allows its developer a rich set of UI controls. Some of the most commonly used
UI Controls are enlisted in the following table.
Let us now learn and understand how to create simple JavaFX programs that use these UI
controls.
Label
• The label control displays simple text on the screen.
• Its main purpose is to describe other components such as textfield, textarea, radio button
and so on.
Label(String txt, Node graphics) : It creates a label with supplied text and graphics.
• Programming Example
package MyJavaFXApplication;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
root.getChildren().add(L);
primaryStage.setTitle("Label Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
Program Explanation: In above program,
(1) We have created StackPane so that the Label control can be arranged on top just like a
stack. The stackPane is a used to decide the layout of the screen. This is named as root
node.
(2) Then we have created one label "User Name" using the Label constructor.
(3) These controls must be contained in a scene. Hence a scene is prepared by adding a root
node using Scene constructor. This scene is then attached to a primaryStage using setScene
method.
(4) Then using show() method we display the primaryStage. This ultimately displays the
label control.
Button
• The button control is the most commonly used control in any GUI application. This UI
component controls the behavior of the application. Some event gets generated when a
button is clicked.
• For using the button control we need to import the package javafx.scene.control.Button.
Following program shows the creation of button control in JavaFX
Programming Example
package MyJavaFXApplication;
import java.io.FileInputStream;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
root.getChildren().add(B);
primaryStage.setTitle("Button Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
Output
Checkbox
Checkbox is used to provide more than one choices at a time.
For using checkbox in our application program, we must insert following line in the program
at the beginning -
import javafx.scene.control.Checkbox
Programming Example
package MyJavaFXApplication;
import java.io.FileInputStream;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
@Override
root.getChildren().addAll(L, ch1,ch2,ch3,ch4);
primaryStage.setTitle("Checkbox Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
ToggleButton
• ToggleButton is a button control that has two states selected or deselected.
• The ToggleButtons are placed in a Toggle Group. From the same group at the most only
one button is selected at a time. If one button is selected from that group then other buttons
of that group remain deselected.
• Constructors
1. ToggleButton(): Creates a toggle button with an empty string for its label.
2. ToggleButton(String txt): Creates a toggle button with the specified text as its label.
Methods
Example Program
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
@Override
root.setPadding(new Insets(15));
root.setSpacing(10);
// Creating a ToggleGroup
blackButton.setToggleGroup(group);
blueButton.setSelected(true);
root.getChildren().addAll(blueButton, blackButton);
primaryStage.setTitle("ToggleButton Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
Output
RadioButtons
The Radio button control is used to make a choice.
The difference between checkbox and radio button is that with checkbox we can have more
than one selection but with radio button we select only one option at a time.
Following example shows how to create and use the radio button control.
Programming Example
package MyJavaFXApplication;
import java.io.FileInputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
rb1.setToggleGroup(group);
rb2.setToggleGroup(group);
rb3.setToggleGroup(group);
rb4.setToggleGroup(group);
root.getChildren().addAll(L,rb1,rb2,rb3,rb4);
primaryStage.setTitle("RadioButton Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
Textfield
TextField control allows the user to enter the text that can be read by the application.
Programming Example
package MyJavaFXApplication;
import java.io.FileInputStream;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
root.addRow(0,L,tf);
primaryStage.setScene(scene);
primaryStage.show();
Output
Sol.:
package MyJavaFXApplication;
import java.io.FileInputStream;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
root.setVgap(10);
root.addRow(0, L1,tf1)
root.addRow(1, L2,tf2)
root.addRow(2,btn);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
TextArea
The TextArea control allows to enter multiline text. This control is represented by the class
javafx.scene.control.TextArea
We can set the size of the TextArea using setPrefHeight() and setPrefWidth() functions.
Programming Example
package My JavaFXApplication;
import java.io.FileInputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
double height=100;
double width=150;
ta.setPrefHeight(height);
ta.setPrefWidth(width);
root.addRow(0,L,ta);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
ListView
The JavaFX ListView control enables users to choose one or more options from a
predefined list of choices. The JavaFX ListView control is represented by the class
javafx.scene.control.ListView.
The items can be added to the ListView control using getItems().add() method. For example
listView.getItems().add("Item 1");
listView.getItems().add("Item 2");
……..
To make a ListView visible we have to add it to the scene graph or to some layout
component which is then attached to the Scene object.
Programming Example
package MyJavaFXApplication;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
listView.getItems().add("Java");
listView.getItems().add("C++");
listView.getItems().add("PHP");
listView.getItems().add("Python");
root.addRow(0,L,listView)
primaryStage.setTitle("ListView Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
Output
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
ComboBox
We can have predefined list of choices using combo box.
Then we need to add the list of choices to the comboBox. This can be done using
cb.getItems().add("Option1")
cb.getItems().add("Option2")
…………..
And so on.
Following program illustrates the use of ComboBox control in the JavaFX application
program.
Programming Example
package MyJavaFXApplication;
import java.io.FileInputStream;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
@Override
cb.getItems().add("Java");
cb.getItems().add("C++");
cb.getItems().add("PHP");
cb.getItems().add("Python");
root.addRow(0,L,cb);
Scene scene = new Scene(root, 350, 200);
primaryStage.setTitle("ComboBox Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
ChoiceBox
• The ChoiceBox control shows the set of items and allows user to select a single choice. It
will show the currently selected item on the top.
• By default the ChoiceBox control has no selected item. We have to specify the items from
which the choice is to be made.
Programming Example
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ChoiceBox;
import javafx.stage.Stage;
@Override
root.setPadding(new Insets(15));
root.setSpacing(10);
// Creating a Label
choiceBox.getItems().add("Blue");
choiceBox.getItems().add("Black");
choiceBox.getItems().add("White");
choiceBox.getItems().add("Red");
root.getChildren().add(choiceBox);
primaryStage.setTitle("ChoiceBox Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
Scrollbar
Using the Scrollbar control the user can scroll down to the application page.
Programming Example
package MyJavaFXApplication;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
public void start (Stage primaryStage) throws Exception {
root.getChildren().add(sb);
Copbolwonhot te
primaryStage.setTitle("ScrollBar Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
We can create the scrollbar in horizontal direction or in vertical direction. If we want the
scrollbar to be displayed vertically then we call setOrientation() method
sb.setOrientation (Orientation. VERTICAL);
Similarly we can set minimum, maximum or current value to scrollbar using following
functions
(1) setMin()
(2) setMax()
(3) setValue()
Following program makes use of these methods for the scrollbar control
package MyJavaFXApplication;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
sb.setMin(0);
sb.setMax(50);
sb.setValue(25);
primaryStage.setTitle("ScrollBar Demo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
Output
Layouts
• The arrangement of various components (nodes) in a scene within the container is called
Layout of the container.
• For using the layout we must import the package javafx.scene.layout. The class named
Pane is the base class for all the layouts in JavaFX.
For example - if we want the spacing between the created nodes then we use
root.setSpacing(10);
root.getChildren().addAll(<NodeObjects>);
FlowPane
• A JavaFX FlowPane is a layout component which lays out its child components either
vertically or horizontally, and which can wrap the components onto the next row or column
if there is not enough space in one row.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
@Override
root.getChildren().add(B1);
root.getChildren().add(B2);
root.getChildren().add(B3);
root.getChildren().add(B4);
root.getChildren().add(B5);
root.getChildren().add(B6);
primaryStage.setScene(scene);
primaryStage.setTitle("FlowPane Demo");
primaryStage.show();
launch(args);
}
HBox
• The HBox layout arranges the children in the form of horizontal rows.
• It requires javafx.scene.layout.HBox class which provides all the required methods of this
pane.
Programming Example
package myjavafxapplication;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
@Override
Output
primaryStage.setScene(scene);
primaryStage.setTitle("HBOX Demo");
primaryStage.show();
launch(args);
}
Program Explanation: In above code,
(2) The layout pane is set as HBox. All the button elements are attached to the HBox pane
by using getChildren().addAll function.
(4) Using setScene method this scene is then added to the PrimaryStage.
VBox
• The VBox layout arranges the children in the form of vertical rows.
• It requires javafx.scene.layout.VBox class which provides all the required methods of this
pane.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
Output
primaryStage.setScene(scene);
primaryStage.setTitle("VBOX Demo");
primaryStage.show();
}
launch(args);
BorderPane
• BorderPane lays out children in top, left, right, bottom, and center positions.
(3) BorderPane (Node center, Node top, Node right, Node bottom, Node left): Creates an
BorderPane layout with the given Nodes to use for each of the main layout areas of the
Border Pane.
Programing Example
package myjavafxapplication;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
@Override
Output
root.setTop(B_top);
root.setCenter(B_center);
root.setBottom (B_bottom);
root.setLeft(B_left);
root.setRight (B_right);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
StackPane
• StackPane is a layout in which every new node is placed on the top of previous node.
1. StackPane()
Programming Example
package myjavafxapplication;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
Output
root.getChildren().addAll(B1,B2,B3);
primaryStage.setScene(scene);
primaryStage.setTitle("StackPane Demo");
primaryStage.show();
launch(args);
GridPane
GridPane places its nodes into a grid of rows and columns. Nodes may span multiple rows
or columns. GridPane is the most flexible built-in layout pane.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
grid.add(B1,0,0);
grid.add(B2,1,0);
grid.add(B3,2,0);
grid.add(B4,0,1);
grid.add(B5,1,1);
Output
grid.add(B6,2,1);
primaryStage.setScene(scene);
primaryStage.setTitle("GridPane Demo");
primaryStage.show();
launch(args);
}
}
Menu
• For using the Menu class in our JavaFX application we need to import
javafx.scene.control. Menu so that the methods associated with this class can be used in the
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 from the menu.
Menu Bars
• MenuBar is just like a navigation bar with menus on it. The menubar is located at the the
screen.
Menultem
Step 1: Create a Menu bar is the first step, MenuBar can be instantiated using new
Step 3: Create the menuitem. The name of the menu is passed as an argument to the
MenuItem class.
Step 4: Add menu items to the menu using add or addAll method
Programming Example
package application;
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;
@Override
// Creating MenuBar
editMenu.getItems().addAll(cutItem,copyItem, pasteItem);
root.setTop(menuBar);
stage.show();
Application.launch(args);
Output