private void showAutocompletePopup(int insertionIndex, String input) { CompletionResultSource suggestionMaker = mySuggestionProvider.get(); List<MenuItem> suggestions = suggestionMaker.getSortedMatches(input, 5) .map(result -> { Label entryLabel = new Label(); entryLabel.setGraphic(result.getTextFlow()); entryLabel.setPrefHeight(5); CustomMenuItem item = new CustomMenuItem(entryLabel, true); item.setUserData(result); item.setOnAction(e -> applySuggestion(insertionIndex, input, result.getNodeName())); return item; }) .collect(Collectors.toList()); autoCompletePopup.getItems().setAll(suggestions); myCodeArea.getCharacterBoundsOnScreen(insertionIndex, insertionIndex + input.length()) .ifPresent(bounds -> autoCompletePopup.show(myCodeArea, bounds.getMinX(), bounds.getMaxY())); Skin<?> skin = autoCompletePopup.getSkin(); if (skin != null) { Node fstItem = skin.getNode().lookup(".menu-item"); if (fstItem != null) { fstItem.requestFocus(); } } }
menu.show(pane, event.getScreenX(), event.getScreenY()); } else if (menu.isShowing()) { menu.hide();
menu.show(xpathExpressionArea, t.getScreenX(), t.getScreenY());
ContextMenu contextMenu = tab.getContextMenu(); if (contextMenu != null) { contextMenu.show(tabLabel, event.getScreenX(), event.getScreenY()); event.consume();
Canvas canvas = ... ; ContextMenu menu = ... ; canvas.setOnContextMenuRequested(e -> menu.show(canvas, e.getScreenX(), e.getScreenY()));
@FXML StackPane stack; // . . . public void initialize() { final ContextMenu contextMenu = new ContextMenu(new MenuItem("xyzzy")); stack.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { contextMenu.show( stack, mouseEvent.getScreenX(), mouseEvent.getScreenY() ); } }); }
ContextMenu visibleMenu = null; tableView.setOnMouseClicked((MouseEvent e) -> { if (visibleMenu !=null) { visibleMenu.hide(); visibleMenu = null; } if (e.getButton()==MouseButton.SECONDARY) { double columnHeaderHeight = tableView.lookup(".column-header-background").getBoundsInLocal().getHeight(); if (e.getY()>columnHeaderHeight) { visibleMenu = getContextMenu(); // build on the fly or use a prebuild menu visibleMenu.show(tableView, e.getScreenX(), e.getScreenY()); } else { // you could show a header specific context menu here } } });
HBox hbox = new HBox(); ContextMenu contextMenu = new ContextMenu(); .... hbox.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() { @Override public void handle(ContextMenuEvent event) { contextMenu.show(hbox, event.getScreenX(), event.getScreenY()); } });
protected EventHandler<MouseEvent> createContextMenuHandler(final ContextMenu contextMenu) { return (MouseEvent event) -> { // TODO: is this check for MouseButton.SECONDARY really necessary? if (event.getButton() == MouseButton.SECONDARY) { contextMenu.show(connectionPath, event.getScreenX(), event.getScreenY()); } }; }
ContextMenu popup=new ContextMenu(); MenuItem itemAdd=new MenuItem("Add/Edit"); MenuItem itemDel=new MenuItem("Delete"); popup.getItems().add(itemAdd); popup.getItems().add(itemDel); txtBox.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent t) { txt=(TextBox)t.getSource(); if(t.getButton()==MouseButton.SECONDARY ) { popup.show(txt,t.getScreenX(),t.getScreenY()); } }
ContextMenu contextMenu = ... TableColumn secondColumn = tableView.getColumns().get(1); tableView.setOnKeyReleased(evt -> { if (evt.getCode() == KeyCode.ENTER) { Set<Node> rows = tableView.lookupAll(".table-row-cell"); Optional<Cell> n = rows.stream().map(r -> (Cell) r).filter(Cell::isSelected).findFirst(); if (n.isPresent()) { Optional<Node> node = n.get().getChildrenUnmodifiable().stream() .filter(c -> c instanceof TableCell && ((TableCell) c).getTableColumn() == secondColumn) .findFirst(); if (node.isPresent()) { Node cell = node.get(); Bounds b = cell.getLayoutBounds(); contextMenu.show(cell, Side.BOTTOM, b.getWidth() / 2, b.getHeight() / -2); } } } });
private static void changeContextMenu(NestedTableColumnHeader header, TableColumn<?, ?> column) { TableColumnHeader headerSkin = scan(column, header); if (headerSkin != null) { headerSkin.setOnContextMenuRequested(ev -> { ContextMenu cMenu = column.getContextMenu(); if (cMenu != null) { cMenu.show(headerSkin, Side.BOTTOM, 5, 5); } ev.consume(); }); } }
ContextMenu cm = new ContextMenu(); MenuItem mi1 = new MenuItem("Menu 1"); cm.getItems().add(mi1); MenuItem mi2 = new MenuItem("Menu 2"); cm.getItems().add(mi2); table.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent t) { if(t.getButton() == MouseButton.SECONDARY) { cm.show(table , t.getScreenX() , t.getScreenY()); } } });
private void showContextMenu(ContextMenuEvent e) { ContextMenu menu = view.getContextMenu(); double xOffset = view.getContextMenuXOffset(); double yOffset = view.getContextMenuYOffset(); menu.show(view, e.getScreenX() + xOffset, e.getScreenY() + yOffset); }
@Override public void start(Stage primaryStage) { MenuItem cmItem1 = new MenuItem("Item 1"); cmItem1.setOnAction(e->System.out.println("Item 1")); MenuItem cmItem2 = new MenuItem("Item 2"); cmItem2.setOnAction(e->System.out.println("Item 2")); final ContextMenu cm = new ContextMenu(cmItem1,cmItem2); Scene scene = new Scene(new StackPane(), 300, 250); primaryStage.setScene(scene); primaryStage.show(); scene.setOnMouseClicked(t -> { if(t.getButton()==MouseButton.SECONDARY){ cm.show(scene.getWindow(),t.getScreenX(),t.getScreenY()); // Request focus on first item cm.getSkin().getNode().lookup(".menu-item").requestFocus(); } }); }
int len = 20; TextField field = new TextField("max chars: " + len ); // here we reject any change which exceeds the length UnaryOperator<Change> rejectChange = c -> { // check if the change might effect the validating predicate if (c.isContentChange()) { // check if change is valid if (c.getControlNewText().length() > len) { // invalid change // sugar: show a context menu with error message final ContextMenu menu = new ContextMenu(); menu.getItems().add(new MenuItem("This field takes\n"+len+" characters only.")); menu.show(c.getControl(), Side.BOTTOM, 0, 0); // return null to reject the change return null; } } // valid change: accept the change by returning it return c; }; field.setTextFormatter(new TextFormatter(rejectChange));
public static void addResetViewMenu(VCanvas canvas) { final ContextMenu cm = new ContextMenu(); MenuItem resetViewItem = new MenuItem("Reset View"); resetViewItem.setOnAction((ActionEvent e) -> { canvas.resetTranslation(); canvas.resetScale(); }); cm.getItems().add(resetViewItem); canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> { if (e.getButton() == javafx.scene.input.MouseButton.SECONDARY) { cm.show(canvas, e.getScreenX(), e.getScreenY()); } }); }
private static <T> void addContextMenu(CheckListView<T> listView) { listView.setOnContextMenuRequested(event -> { ContextMenu contextMenu = new ContextMenu(); MenuItem selectAll = new MenuItem(RESOURCE_BUNDLE.getString("SelectAll")); selectAll.disableProperty().bind(Bindings.equal(listView.getItems().size(), Bindings.size(listView.getCheckModel().getCheckedIndices()))); selectAll.setOnAction(event2 -> listView.getCheckModel().checkAll()); MenuItem deselectAll = new MenuItem(RESOURCE_BUNDLE.getString("DeselectAll")); deselectAll.disableProperty().bind(Bindings.isEmpty(listView.getCheckModel().getCheckedIndices())); deselectAll.setOnAction(event2 -> listView.getCheckModel().clearChecks()); contextMenu.getItems().addAll(selectAll, deselectAll); contextMenu.show(listView, event.getScreenX(), event.getScreenY()); }); }