Manually Creating a Menu Bar in Java


/ Published in: Java
Save to your folder(s)

Cumbersome but effective code to creating a menu bar


Copy this code and paste it in your HTML
  1. import javafx.application.Application;
  2. import javafx.geometry.Insets;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.*;
  5. import javafx.scene.image.Image;
  6. import javafx.scene.image.ImageView;
  7. import javafx.scene.input.KeyCombination;
  8. import javafx.scene.input.MouseButton;
  9. import javafx.scene.layout.BackgroundFill;
  10. import javafx.scene.layout.BorderPane;
  11. import javafx.scene.layout.CornerRadii;
  12. import javafx.scene.layout.VBox;
  13. import javafx.scene.paint.Color;
  14. import javafx.scene.paint.CycleMethod;
  15. import javafx.scene.paint.LinearGradient;
  16. import javafx.scene.paint.Stop;
  17. import javafx.scene.shape.Rectangle;
  18. import javafx.stage.Stage;
  19.  
  20. public class MenuExample extends Application {
  21. public static void main(String[] args) {
  22. launch(args);
  23. }
  24.  
  25. private Stage stage = null;
  26.  
  27. public Menu createFileMenu() {
  28. Menu fileMenu = new Menu("File");
  29.  
  30. Image icon = new Image(Example01.class.getResourceAsStream("/images/new.png"));
  31. MenuItem itemNew = new MenuItem("New...", new ImageView(icon));
  32. itemNew.setAccelerator(KeyCombination.keyCombination("Ctrl+N"));
  33. itemNew.setOnAction(event -> System.out.println("New... menu item clicked."));
  34.  
  35. // create open menu item
  36. MenuItem itemOpen = new MenuItem("Open...");
  37. itemOpen.setOnAction(event -> System.out.println("Open... menu item clicked."));
  38.  
  39. // create exit menu item
  40. MenuItem itemExit = new MenuItem("Exit");
  41. itemExit.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
  42. itemExit.setOnAction(event -> {
  43. System.out.println("Exit menu item clicked.");
  44. // stop the application
  45. stage.hide();
  46. });
  47.  
  48. // add menu items to the menu
  49. fileMenu.getItems().addAll(
  50. itemNew,
  51. itemOpen,
  52. itemExit
  53. );
  54.  
  55. return fileMenu;
  56. }
  57.  
  58. public Menu createEditMenu() {
  59. Menu editMenu = new Menu("Edit");
  60.  
  61. MenuItem itemCopy = new MenuItem("Copy");
  62. itemCopy.setOnAction(event -> System.out.println("Copy menu item clicked."));
  63.  
  64. MenuItem itemCut = new MenuItem("Cut");
  65. itemCut.setOnAction(event -> System.out.println("Cut menu item clicked."));
  66.  
  67. // add menu items to the menu
  68. editMenu.getItems().addAll(
  69. itemCopy,
  70. itemCut,
  71. createPasteMenu()
  72. );
  73.  
  74. return editMenu;
  75. }
  76.  
  77. public Menu createPasteMenu() {
  78. Menu pasteMenu = new Menu("Paste as");
  79.  
  80. MenuItem itemPasteAsText = new MenuItem("Text");
  81. itemPasteAsText.setOnAction(event -> System.out.println("Paste as Text menu item clicked."));
  82.  
  83. MenuItem itemPasteAsContent = new MenuItem("Content");
  84. itemPasteAsContent.setOnAction(event -> System.out.println("Paste as Content menu item clicked."));
  85.  
  86. MenuItem itemPasteAsFormatting = new MenuItem("Formatting");
  87. itemPasteAsFormatting.setOnAction(event -> System.out.println("Paste as Formatting menu item clicked."));
  88.  
  89.  
  90. // add menu items to the menu
  91. pasteMenu.getItems().addAll(
  92. itemPasteAsText,
  93. itemPasteAsContent,
  94. itemPasteAsFormatting
  95. );
  96.  
  97. return pasteMenu;
  98. }
  99.  
  100. public MenuBar createMenuBar() {
  101. MenuBar menuBar = new MenuBar();
  102.  
  103. menuBar.getMenus().addAll(
  104. createFileMenu(),
  105. createEditMenu()
  106. );
  107.  
  108. return menuBar;
  109. }
  110.  
  111. public ContextMenu createContextMenu() {
  112. ContextMenu contextMenu = new ContextMenu(
  113. createFileMenu(),
  114. createEditMenu()
  115. );
  116.  
  117. return contextMenu;
  118. }
  119.  
  120. public Scene createScene() {
  121. BorderPane bp = new BorderPane();
  122.  
  123. VBox center = new VBox();
  124. center.setSpacing(10);
  125.  
  126. Rectangle rect = createRectangle();
  127.  
  128. center.getChildren().addAll(
  129. createMenuButton(),
  130. createSplitMenuButton(),
  131. rect
  132. );
  133.  
  134. // make the context menu appear when right-click on the center VBox
  135. ContextMenu contextMenu = createContextMenu();
  136. rect.setOnMousePressed(event -> {
  137. if (event.getButton() == MouseButton.SECONDARY) {
  138. contextMenu.show(stage, event.getScreenX(), event.getScreenY());
  139. }
  140. });
  141.  
  142. // set the menu bar to the top section, rest in center
  143. bp.setTop(createMenuBar());
  144. bp.setCenter(center);
  145.  
  146. return new Scene(bp, 500, 400);
  147. }
  148.  
  149. @Override
  150. public void start(Stage primaryStage) throws Exception {
  151. stage = primaryStage;
  152.  
  153. primaryStage.setTitle("Example 1");
  154. primaryStage.setScene(createScene());
  155. primaryStage.show();
  156. }
  157. }

URL: manualmenujava

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.