Using Menu to control a page : Menu « JavaFX « Java






Using Menu to control a page

 

/*
 * Copyright (c) 2011, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the distribution.
 *  - Neither the name of Oracle nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
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.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Glow;
import javafx.scene.effect.SepiaTone;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class Main extends Application {

  final PageData[] pages = new PageData[] {
      new PageData(
          "Apple",
          "The apple is the pomaceous fruit of the apple tree, species Malus "
              + "domestica in the rose family (Rosaceae). It is one of the most "
              + "widely cultivated tree fruits, and the most widely known of "
              + "the many members of genus Malus that are used by humans. "
              + "The tree originated in Western Asia, where its wild ancestor, "
              + "the Alma, is still found today.", "Malus domestica"),
      new PageData(
          "Hawthorn",
          "The hawthorn is a large genus of shrubs and trees in the rose "
              + "family, Rosaceae, native to temperate regions of the Northern "
              + "Hemisphere in Europe, Asia and North America. "
              + "The name hawthorn was "
              + "originally applied to the species native to northern Europe, "
              + "especially the Common Hawthorn C. monogyna, and the unmodified "
              + "name is often so used in Britain and Ireland.",
          "Crataegus monogyna"),
      new PageData(
          "Ivy",
          "The ivy is a flowering plant in the grape family (Vitaceae) native"
              + " to eastern Asia in Japan, Korea, and northern and eastern China."
              + " It is a deciduous woody vine growing to 30 m tall or more given "
              + "suitable support,  attaching itself by means of numerous small "
              + "branched tendrils tipped with sticky disks.",
          "Parthenocissus tricuspidata"),
      new PageData(
          "Quince",
          "The quince is the sole member of the genus Cydonia and is native"
              + " to warm-temperate southwest Asia in the Caucasus region. The "
              + "immature fruit is green with dense grey-white pubescence, most "
              + "of which rubs off before maturity in late autumn when the fruit "
              + "changes color to yellow with hard, strongly perfumed flesh.",
          "Cydonia oblonga") };

  final String[] viewOptions = new String[] { "Title", "Binomial name",
      "Picture", "Decsription" };

  final Entry<String, Effect>[] effects = new Entry[] {
      new SimpleEntry<String, Effect>("Sepia Tone", new SepiaTone()),
      new SimpleEntry<String, Effect>("Glow", new Glow()),
      new SimpleEntry<String, Effect>("Shadow", new DropShadow()) };

  final ImageView pic = new ImageView();
  final Label name = new Label();
  final Label binName = new Label();
  final Label description = new Label();
  private int currentIndex = -1;

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

  @Override
  public void start(Stage stage) {
    stage.setTitle("Menu Sample");
    Scene scene = new Scene(new VBox(), 400, 350);
    scene.setFill(Color.OLDLACE);

    name.setFont(new Font("Verdana Bold", 22));
    binName.setFont(new Font("Arial Italic", 10));
    pic.setFitHeight(150);
    pic.setPreserveRatio(true);
    description.setWrapText(true);
    description.setTextAlignment(TextAlignment.JUSTIFY);

    shuffle();

    MenuBar menuBar = new MenuBar();

    // --- Graphical elements
    final VBox vbox = new VBox();
    vbox.setAlignment(Pos.CENTER);
    vbox.setSpacing(10);
    vbox.setPadding(new Insets(0, 10, 0, 10));
    vbox.getChildren().addAll(name, binName, pic, description);

    // --- Menu File
    Menu menuFile = new Menu("File");
    MenuItem add = new MenuItem("Shuffle", new ImageView(new Image(
        "src/menusample/new.png")));
    add.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent t) {
        shuffle();
        vbox.setVisible(true);
      }
    });

    MenuItem clear = new MenuItem("Clear");
    clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
    clear.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent t) {
        vbox.setVisible(false);
      }
    });

    MenuItem exit = new MenuItem("Exit");
    exit.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent t) {
        System.exit(0);
      }
    });

    menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);

    // --- Menu Edit
    Menu menuEdit = new Menu("Edit");
    Menu menuEffect = new Menu("Picture Effect");

    final ToggleGroup groupEffect = new ToggleGroup();
    for (Entry effect : effects) {
      RadioMenuItem itemEffect = new RadioMenuItem((String)effect.getKey());
      itemEffect.setUserData(effect.getValue());
      itemEffect.setToggleGroup(groupEffect);
      menuEffect.getItems().add(itemEffect);
    }

    final MenuItem noEffects = new MenuItem("No Effects");
    noEffects.setDisable(true);
    noEffects.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent t) {
        pic.setEffect(null);
        groupEffect.getSelectedToggle().setSelected(false);
        noEffects.setDisable(true);
      }
    });

    groupEffect.selectedToggleProperty().addListener(
        new ChangeListener<Toggle>() {
          public void changed(ObservableValue ov, Toggle old_toggle,
              Toggle new_toggle) {
            if (groupEffect.getSelectedToggle() != null) {
              Effect effect = (Effect) groupEffect.getSelectedToggle()
                  .getUserData();
              pic.setEffect(effect);
              noEffects.setDisable(false);
            } else {
              noEffects.setDisable(true);
            }
          }
        });

    menuEdit.getItems().addAll(menuEffect, noEffects);

    // --- Menu View
    Menu menuView = new Menu("View");
    CheckMenuItem titleView = createMenuItem("Title", name);
    CheckMenuItem binNameView = createMenuItem("Binomial name", binName);
    CheckMenuItem picView = createMenuItem("Picture", pic);
    CheckMenuItem descriptionView = createMenuItem("Decsription", description);

    menuView.getItems()
        .addAll(titleView, binNameView, picView, descriptionView);
    menuBar.getMenus().addAll(menuFile, menuEdit, menuView);

    // --- Context Menu
    final ContextMenu cm = new ContextMenu();
    MenuItem cmItem1 = new MenuItem("Copy Image");
    cmItem1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent content = new ClipboardContent();
        content.putImage(pic.getImage());
        clipboard.setContent(content);
      }
    });

    cm.getItems().add(cmItem1);
    pic.addEventHandler(MouseEvent.MOUSE_CLICKED,
        new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent e) {
            if (e.getButton() == MouseButton.SECONDARY)
              cm.show(pic, e.getScreenX(), e.getScreenY());
          }
        });

    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);

    stage.setScene(scene);
    stage.show();
  }

  private void shuffle() {
    int i = currentIndex;
    while (i == currentIndex) {
      i = (int) (Math.random() * pages.length);
    }
    pic.setImage(pages[i].image);
    name.setText(pages[i].name);
    binName.setText("(" + pages[i].binNames + ")");
    description.setText(pages[i].description);
    currentIndex = i;
  }

  private static CheckMenuItem createMenuItem(String title, final Node node) {
    CheckMenuItem cmi = new CheckMenuItem(title);
    cmi.setSelected(true);
    cmi.selectedProperty().addListener(new ChangeListener<Boolean>() {
      public void changed(ObservableValue ov, Boolean old_val, Boolean new_val) {
        node.setVisible(new_val);
      }
    });
    return cmi;
  }

  private class PageData {
    public String name;
    public String description;
    public String binNames;
    public Image image;

    public PageData(String name, String description, String binNames) {
      this.name = name;
      this.description = description;
      this.binNames = binNames;
      image = new Image(getClass().getResourceAsStream(name + ".jpg"));
    }
  }
}

   
  








Related examples in the same category

1.Add MenuItem to Menu and then add menu to MenuBar
2.Using submenu
3.Set accelerator for Menu
4.Menu with EventHandler
5.Menu Item ToggleGroup
6.Set setMnemonicParsing