Example usage for javafx.scene.control ContextMenu show

List of usage examples for javafx.scene.control ContextMenu show

Introduction

In this page you can find the example usage for javafx.scene.control ContextMenu show.

Prototype

@Override
public void show(Node anchor, double screenX, double screenY) 

Source Link

Document

Shows the ContextMenu at the specified screen coordinates.

Usage

From source file:Main.java

@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);/*  w ww. j  a v  a  2  s . c o m*/
    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();
}

From source file:de.pixida.logtest.designer.automaton.Graph.java

public void onMouseClicked(final BaseObject baseObject, final MouseEvent event) {
    Validate.notNull(baseObject);/*from  w  w w . jav  a2  s.  c om*/

    this.contextMenu.hide();

    if (event.getButton() == MouseButton.PRIMARY) {
        if (this.connectorSourceNode != null) {
            Validate.notNull(this.connector);
            if (baseObject == this.currentConnectorTargetNode) {
                this.connector.attach(this.connectorSourceNode, this.currentConnectorTargetNode);

                this.connector = null;
                this.connectorSourceNode = null;
                this.currentConnectorTargetNode = null;

                this.handleChange();
            }
        }
        event.consume();
    } else if (event.getButton() == MouseButton.SECONDARY) {
        final ContextMenu objectContextMenu = baseObject.createContextMenu();
        if (objectContextMenu != null) {
            Node contextMenuOwner = baseObject.getActionHandler();
            if (contextMenuOwner == null) {
                contextMenuOwner = this.pane;
            }
            objectContextMenu.show(contextMenuOwner, event.getScreenX(), event.getScreenY());
            event.consume();
        }
    }
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    TextField notification = new TextField();

    MenuItem item1 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("About");
        }//from  ww w  . ja v  a2s .c o  m
    });
    MenuItem item2 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("Preferences");
        }
    });

    final ContextMenu contextMenu = new ContextMenu(item1, item2);
    contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
        public void handle(WindowEvent e) {
            System.out.println("showing");
        }
    });
    contextMenu.setOnShown(new EventHandler<WindowEvent>() {
        public void handle(WindowEvent e) {
            System.out.println("shown");
        }
    });

    System.out.println(contextMenu.onActionProperty());
    contextMenu.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("on");
        }
    });

    notification.setContextMenu(contextMenu);
    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("To: "), 0, 0);
    grid.add(notification, 1, 0);

    contextMenu.show(grid, 20, 20);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@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);/*from   w ww.  j  a v a 2s.  c  o  m*/
    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();
}

From source file:dpfmanager.shell.interfaces.gui.component.report.ReportsView.java

public void addFormatIcons() {
    colFormats.setCellFactory(/*from   www  .  ja v  a 2  s .c  o m*/
            new Callback<TableColumn<ReportRow, ObservableMap<String, String>>, TableCell<ReportRow, ObservableMap<String, String>>>() {
                @Override
                public TableCell<ReportRow, ObservableMap<String, String>> call(
                        TableColumn<ReportRow, ObservableMap<String, String>> param) {
                    TableCell<ReportRow, ObservableMap<String, String>> cell = new TableCell<ReportRow, ObservableMap<String, String>>() {
                        @Override
                        public void updateItem(ObservableMap<String, String> item, boolean empty) {
                            super.updateItem(item, empty);
                            if (!empty && item != null) {

                                HBox box = new HBox();
                                box.setSpacing(3);
                                box.setAlignment(Pos.CENTER_LEFT);

                                for (String i : item.keySet()) {
                                    ImageView icon = new ImageView();
                                    icon.setId("but" + i);
                                    icon.setFitHeight(20);
                                    icon.setFitWidth(20);
                                    icon.setImage(new Image("images/formats/" + i + ".png"));
                                    icon.setCursor(Cursor.HAND);

                                    String type = i;
                                    String path = item.get(i);
                                    icon.setOnMouseClicked(new EventHandler<MouseEvent>() {
                                        @Override
                                        public void handle(MouseEvent event) {
                                            ArrayMessage am = new ArrayMessage();
                                            am.add(GuiConfig.PERSPECTIVE_SHOW, new UiMessage());
                                            am.add(GuiConfig.PERSPECTIVE_SHOW + "." + GuiConfig.COMPONENT_SHOW,
                                                    new ShowMessage(type, path));
                                            getContext().send(GuiConfig.PERSPECTIVE_SHOW, am);
                                        }
                                    });

                                    ContextMenu contextMenu = new ContextMenu();
                                    javafx.scene.control.MenuItem download = new javafx.scene.control.MenuItem(
                                            "Download report");
                                    contextMenu.getItems().add(download);
                                    icon.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
                                        public void handle(ContextMenuEvent e) {
                                            contextMenu.show(icon, e.getScreenX(), e.getScreenY());
                                        }
                                    });
                                    box.getChildren().add(icon);
                                }

                                setGraphic(box);
                            } else {
                                setGraphic(null);
                            }
                        }
                    };
                    return cell;
                }
            });
}

From source file:net.sourceforge.pmd.util.fxdesigner.XPathPanelController.java

private void autoComplete(int slashPosition, String input, ContextMenu autoCompletePopup) {

    XPathSuggestions xPathSuggestions = new XPathSuggestions(parent.getLanguageVersion().getLanguage());
    List<String> suggestions = xPathSuggestions.getXPathSuggestions(input.trim());

    List<CustomMenuItem> resultToDisplay = new ArrayList<>();
    if (!suggestions.isEmpty()) {

        for (int i = 0; i < suggestions.size() && i < 5; i++) {
            final String searchResult = suggestions.get(i);

            Label entryLabel = new Label();
            entryLabel.setGraphic(highlightXPathSuggestion(suggestions.get(i), input));
            entryLabel.setPrefHeight(5);
            CustomMenuItem item = new CustomMenuItem(entryLabel, true);
            resultToDisplay.add(item);/*from  w ww . j a v a2s  .  c om*/

            item.setOnAction(e -> {
                xpathExpressionArea.replaceText(slashPosition, slashPosition + input.length(), searchResult);
                autoCompletePopup.hide();
            });
        }
    }
    autoCompletePopup.getItems().setAll(resultToDisplay);

    xpathExpressionArea.getCharacterBoundsOnScreen(slashPosition, slashPosition + input.length()).ifPresent(
            bounds -> autoCompletePopup.show(xpathExpressionArea, bounds.getMinX(), bounds.getMaxY()));
}

From source file:View.Visualize.java

private void addColorChangeOnIndividual(ObservableList<XYChart.Data> data) {

    final ContextMenu contextMenu = new ContextMenu();
    MenuItem changeColor = new MenuItem("Change Color");
    MenuItem delete = new MenuItem("Standard color");

    ColorPicker cp = new ColorPicker();
    changeColor.setGraphic(cp);// w w w . ja va 2  s.  c o m
    contextMenu.getItems().addAll(changeColor, delete);

    for (XYChart.Data d : data) {

        d.getNode().setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                if (t.getButton() == MouseButton.SECONDARY) {
                    delete.setOnAction(new EventHandler() {
                        public void handle(Event t) {
                            d.getNode().setStyle("");
                        }

                    });

                    cp.setValue(null);
                    cp.setOnAction(new EventHandler() {
                        public void handle(Event t) {
                            String hex1 = "#" + Integer.toHexString(cp.getValue().hashCode());

                            d.getNode().setStyle("-fx-background-color: " + hex1 + ";");
                        }
                    });

                    contextMenu.show(d.getNode(), t.getScreenX(), t.getScreenY());
                }
            }

        });
    }

}

From source file:View.Visualize.java

public void getPieChartData(Integer nameColumn, Integer valueColumn, Table table, PieChart pieChart, Label lbl,
        Boolean newSeries, Boolean rowCounter) {
    data.clear();/* www .  jav a 2  s .c  o  m*/

    if (!newSeries) {
        pieChart.getData().clear();
    }

    addDataFromTable(table, nameColumn, valueColumn, rowCounter);

    data.entrySet().stream().map(entry -> new PieChart.Data(entry.getKey(), entry.getValue()))
            .forEach(pieChart.getData()::add);

    for (PieChart.Data d : pieChart.getData()) {
        //deretter legger vi animasjon p piecharten.. 
        d.getNode().setOnMouseClicked(new mouseHooverAnimationPieChart.MouseHoverAnimation(d, pieChart));
        final Node n = d.getNode();
        Tooltip tooltip = new Tooltip();
        String toolTipText = "Value : " + d.getPieValue();
        tooltip.setText(toolTipText);
        Tooltip.install(n, tooltip);
        n.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent e) {
                n.setEffect(glow);
            }
        });
        n.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent e) {
                n.setEffect(null);
            }
        });
        final ContextMenu contextMenu = new ContextMenu();
        MenuItem changeColor = new MenuItem("Change Color");
        MenuItem delete = new MenuItem("Standard color");
        ColorPicker cp = new ColorPicker();
        changeColor.setGraphic(cp);
        contextMenu.getItems().addAll(changeColor, delete);

        d.getNode().setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                if (t.getButton() == MouseButton.SECONDARY) {
                    delete.setOnAction(new EventHandler() {
                        public void handle(Event t) {
                            d.getNode().setStyle("");
                        }

                    });

                    cp.setValue(null);
                    cp.setOnAction(new EventHandler() {
                        public void handle(Event t) {
                            String hex1 = "#" + Integer.toHexString(cp.getValue().hashCode());

                            d.getNode().setStyle("-fx-background-color: " + hex1 + ";");
                        }
                    });

                    contextMenu.show(d.getNode(), t.getScreenX(), t.getScreenY());
                }
            }

        });

    }

}

From source file:com.github.drbookings.ui.controller.MainController.java

private void initTableViewContextMenus() {
    final ContextMenu menu = new ContextMenu();
    final MenuItem mi1 = new MenuItem("Delete");
    final MenuItem mi2 = new MenuItem("Add");
    final MenuItem mi3 = new MenuItem("Modify");
    mi1.setOnAction(event -> {/* w w  w. j  a va 2s  .c o m*/
        Platform.runLater(() -> deleteSelected());
    });
    mi2.setOnAction(event -> {
        Platform.runLater(() -> addBooking());
    });
    mi3.setOnAction(event -> {
        Platform.runLater(() -> showModifyBookingDialog());
    });
    menu.getItems().addAll(mi2, mi1, mi3);

    tableView.setContextMenu(menu);
    tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, t -> {
        if (t.getButton() == MouseButton.SECONDARY) {
            menu.show(tableView, t.getScreenX(), t.getScreenY());
        }
    });
}

From source file:net.sourceforge.pmd.util.fxdesigner.XPathPanelController.java

private void initGenerateXPathFromStackTrace() {

    ContextMenu menu = new ContextMenu();

    MenuItem item = new MenuItem("Generate from stack trace...");
    item.setOnAction(e -> {//from   w ww.  j  av a2  s.c  om
        try {
            Stage popup = new Stage();
            FXMLLoader loader = new FXMLLoader(DesignerUtil.getFxml("generate-xpath-from-stack-trace.fxml"));
            Parent root = loader.load();
            Button button = (Button) loader.getNamespace().get("generateButton");
            TextArea area = (TextArea) loader.getNamespace().get("stackTraceArea");

            ValidationSupport validation = new ValidationSupport();

            validation.registerValidator(area,
                    Validator.createEmptyValidator("The stack trace may not be empty"));
            button.disableProperty().bind(validation.invalidProperty());

            button.setOnAction(f -> {
                DesignerUtil.stackTraceToXPath(area.getText()).ifPresent(xpathExpressionArea::replaceText);
                popup.close();
            });

            popup.setScene(new Scene(root));
            popup.initStyle(StageStyle.UTILITY);
            popup.initModality(Modality.WINDOW_MODAL);
            popup.initOwner(designerRoot.getMainStage());
            popup.show();
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    });

    menu.getItems().add(item);

    xpathExpressionArea.addEventHandler(MouseEvent.MOUSE_CLICKED, t -> {
        if (t.getButton() == MouseButton.SECONDARY) {
            menu.show(xpathExpressionArea, t.getScreenX(), t.getScreenY());
        }
    });
}