0% found this document useful (0 votes)
42 views16 pages

Javafx Menus

javafx menus

Uploaded by

sri20049944
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)
42 views16 pages

Javafx Menus

javafx menus

Uploaded by

sri20049944
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

JAVAFX MENUS

• JavaFX provides a Menu class to implement menus. Menu is the main


component of a any application.
• In JavaFX, [Link] class provides all the methods
to deal with menus. This class needs to be instantiated to create a
Menu.
• Menu is a popup menu that contains several menu items that are
displayed when the user clicks a menu. The user can select a
menu item after which the menu goes into a hidden state.
• MenuBar is usually placed at the top of the screen which
contains several menus. JavaFX MenuBar is typically an
implementation of a menu bar.
The following sample of code shows the implementation of JavaFX
menu.
• ManuBar menubar = new MenuBar(); //creating MenuBar
• Menu MenuName = new Menu("Menu Name"); //creating Menu
• MenuItem MenuItem1 = new MenuItem("Menu Item 1 Name");
//creating Menu Item
• [Link]().add(MenuItem1);
//adding Menu Item to the Menu
• [Link]().add(MenuName);
//adding Menu to the MenuBar
Constructors of menu classes 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.
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.

setOnShown(EventHandler v Sets the value of the property onShown.


import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class MenuExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
BorderPane root = new BorderPane();
Scene scene = new Scene(root,200,300);
MenuBar menubar = new MenuBar();
Menu FileMenu = new Menu("File");
MenuItem filemenu1=new MenuItem("new");
MenuItem filemenu2=new MenuItem("Save");
MenuItem filemenu3=new MenuItem("Exit");
Menu EditMenu=new Menu("Edit");
MenuItem EditMenu1=new MenuItem("Cut");
MenuItem EditMenu2=new MenuItem("Copy");
MenuItem EditMenu3=new MenuItem("Paste");
[Link]().addAll(EditMenu1,EditMenu2,EditMenu3);
[Link](menubar);
[Link]().addAll(filemenu1,filemenu2,filemenu3);
[Link]().addAll(FileMenu,EditMenu);
[Link](scene);
[Link]();

}
}
OUTPUT
QUESTION
Java program to create a menu bar and add menu to it
and also add menuitems to the menu: This program
creates a menubar indicated by the name mb. A menu will
be created by name m and 3 menuitems m1, m2, m3 will
be added to the menu m and the menu m will be added to
menubar mb. The menubar will be created inside a scene,
which in turn will be hosted inside a stage. The function
setTitle() is used to provide title to the stage. Then a VBox
is created, on which addChildren() method is called to
attach the menubar inside the scene. Finally, the show()
method is called to display the final results.
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
public class MenuBar_1 extends Application {
public void start(Stage s) {
[Link]("creating MenuBar");
Menu m = new Menu("Menu");
MenuItem m1 = new MenuItem("menu item 1");
MenuItem m2 = new MenuItem("menu item 2");
MenuItem m3 = new MenuItem("menu item 3");
[Link]().add(m1);
[Link]().add(m2);
[Link]().add(m3);
MenuBar mb = new MenuBar();
// add menu to menubar
[Link]().add(m);
// create a VBox
VBox vb = new VBox(mb);

// create a scene
Scene sc = new Scene(vb, 500, 300);
// set the scene
[Link](sc);
[Link]();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
OUTPUT
QUESTION
Java program to create a menu bar and add a menu to it and also add
menu items to menu and also add an event listener to handle the
events: This program creates a menubar indicated by the name mb. A
menu will be created by name m and 3 menuitems m1, m2, m3 will be
added to the menu m and the menu m will be added to the
menubar mb. The menubar will be created inside a scene, which in
turn will be hosted inside a stage. The function setTitle() is used to
provide title to the stage. Then a VBox is created, on which
addChildren() method is called to attach the menubar inside the scene.
Finally, the show() method is called to display the final results. A label
will also be created that will show which menuitem is selected. An
action event will be created to process the action when the menu item
is clicked by the user.
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
public class MenuBar_2 extends Application {
public void start(Stage s) {
[Link]("creating MenuBar");
Menu m = new Menu("Menu");
MenuItem m1 = new MenuItem("menu item 1");
MenuItem m2 = new MenuItem("menu item 2");
MenuItem m3 = new MenuItem("menu item 3");
[Link]().add(m1);
[Link]().add(m2);
[Link]().add(m3);
Label l = new Label("\t\t\t\t" + "no menu item selected");
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
[Link]("\t\t\t\t" + ((MenuItem)[Link]()).getText() +
" selected");
}
};
[Link](event);
[Link](event);
[Link](event);
MenuBar mb = new MenuBar();
[Link]().add(m);
VBox vb = new VBox(mb, l);
Scene sc = new Scene(vb, 500, 300);
[Link](sc);
[Link]();
}
public static void main(String args[])
{
launch(args);
}
}
OUTPUT

You might also like