Friday, 25 September 2015

Tutorial On Menubar With Custom MenuItem In JavaFx

This tutorial will explain you how to create menu bar,how to add menu item in menu bar,how to  and how to add custom menu item in menu bar in java fx.Following are the steps for creating menu bar with menu items.

  1. Create menu bar and then add it on node.
  2. Create menu and add it into menu bar.
  3. Create menu item and add it in menu.
  4. If you want to create sub-menu item then first create it and then add into a menu item.



CODING:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class fxmain extends Application 
{
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("First menu");
MenuItem firstmenuitem = new MenuItem("FirstMenu Item");
Menu secondmenuitem = new Menu("SecondMenu Item");
MenuItem thirdmenuitem = new MenuItem("ThirdMenu Item");
MenuItem fourthmenuitem = new MenuItem("ThirdMenu Item");
MenuItem firstsubmenu = new MenuItem("FirstSubMenu");
MenuItem secondsubmenu = new MenuItem("SeconSubMenu");
CustomMenuItem slide = new CustomMenuItem(new Slider());

@Override
public void start(Stage primaryStage)
 {

menuBar.getMenus().add(menu);
menu.getItems().addAll(firstmenuitem,secondmenuitem,thirdmenuitem,new  SeparatorMenuItem(),slide);
secondmenuitem.getItems().addAll(firstsubmenu,secondsubmenu);

Group root = new Group();
root.getChildren().add(menuBar);
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Scene scene = new Scene(root, 300, 250,Color.OLDLACE);

primaryStage.setTitle("Menu In Fx");
primaryStage.setScene(scene);
primaryStage.show();
}


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

}

OUTPUT:



No comments:

Post a Comment