JavaFX Menu create

Description

JavaFX Menu create

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

   public static void main(String[] args) {
      Application.launch(args);// ww  w.  j a v  a2 s.com
   }

   @Override
   public void start(Stage primaryStage) {
      primaryStage.setTitle("Creating Menus");
      Group root = new Group();
      Scene scene = new Scene(root, 300, 250, Color.WHITE);

      MenuBar menuBar = new MenuBar();

      // File menu - new, save, exit
      Menu menu = new Menu("File");
      menu.getItems().add(new MenuItem("New"));
      menu.getItems().add(new MenuItem("Save"));
      menu.getItems().add(new SeparatorMenuItem());
      menu.getItems().add(new MenuItem("Exit"));

      menuBar.getMenus().add(menu);

      // Cameras menu - camera 1, camera 2
      Menu tools = new Menu("Cameras");
      CheckMenuItem i = new CheckMenuItem();
      i.setText("Show Camera 1");
      i.setSelected(true);
      tools.getItems().add(i);

      i = new CheckMenuItem();
      i.setText("Show Camera 2");
      i.setSelected(true);
      
      tools.getItems().add(i);
      menuBar.getMenus().add(tools);

      // Alarm
      Menu alarm = new Menu("Alarm");
      ToggleGroup tGroup = new ToggleGroup();
      RadioMenuItem soundAlarmItem = new RadioMenuItem();
      soundAlarmItem.setToggleGroup(tGroup);
      soundAlarmItem.setText("Sound Alarm");
      
      RadioMenuItem stopAlarmItem = new RadioMenuItem();
      stopAlarmItem.setToggleGroup(tGroup);
      stopAlarmItem.setText("Alarm Off");
      stopAlarmItem.setSelected(true);

      alarm.getItems().add(soundAlarmItem);
      alarm.getItems().add(stopAlarmItem);

      Menu contingencyPlans = new Menu("Language");
      contingencyPlans.getItems().add(new CheckMenuItem("CSS"));
      contingencyPlans.getItems().add(new CheckMenuItem("Java"));
      contingencyPlans.getItems().add(new CheckMenuItem("Javascript"));

      alarm.getItems().add(contingencyPlans);
      menuBar.getMenus().add(alarm);

      menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

      root.getChildren().add(menuBar);
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related