Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContextMenu;
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.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    private MenuItem exitMenuItem() {
        MenuItem exitMenuItem = new MenuItem("Exit");
        exitMenuItem.setOnAction(actionEvent -> Platform.exit());

        return exitMenuItem;
    }

    private Menu fileMenu() {
        Menu fileMenu = new Menu("File");
        MenuItem newMenuItem = new MenuItem("New");
        MenuItem saveMenuItem = new MenuItem("Save");
        fileMenu.getItems().addAll(newMenuItem, saveMenuItem, new SeparatorMenuItem(), exitMenuItem());
        return fileMenu;
    }

    private Menu cameraMenu() {
        Menu cameraMenu = new Menu("Cameras");

        CheckMenuItem cam1MenuItem = new CheckMenuItem("Show Camera 1");
        cam1MenuItem.setSelected(true);

        CheckMenuItem cam2MenuItem = new CheckMenuItem("Show Camera 2");
        cam2MenuItem.setSelected(true);

        cameraMenu.getItems().addAll(cam1MenuItem, cam2MenuItem);

        return cameraMenu;
    }

    private Menu alarmMenu() {
        Menu alarmMenu = new Menu("Alarm");
        RadioMenuItem soundAlarmItem = new RadioMenuItem("Sound Alarm");
        RadioMenuItem stopAlarmItem = new RadioMenuItem("Alarm Off");

        Menu contingencyPlans = new Menu("Plans");
        contingencyPlans.getItems().addAll(new CheckMenuItem("A"), new CheckMenuItem("B"), new CheckMenuItem("C"));
        alarmMenu.getItems().add(contingencyPlans);
        ToggleGroup tGroup = new ToggleGroup();
        soundAlarmItem.setToggleGroup(tGroup);
        stopAlarmItem.setToggleGroup(tGroup);
        stopAlarmItem.setSelected(true);
        alarmMenu.getItems().addAll(soundAlarmItem, stopAlarmItem, new SeparatorMenuItem());
        return alarmMenu;
    }

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().addAll(fileMenu(), cameraMenu(), alarmMenu());
        root.setTop(menuBar);
        ContextMenu contextFileMenu = new ContextMenu(exitMenuItem());

        primaryStage.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me) -> {
            if (me.getButton() == MouseButton.SECONDARY || me.isControlDown()) {
                contextFileMenu.show(root, me.getScreenX(), me.getScreenY());
            } else {
                contextFileMenu.hide();
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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