adding context menu to pane in javafx - java

i'am trying to make context menu that will show when i click right mouse click
but it's look like i just can add the context menu to the controls only
here is me code
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
MenuItem cut = new MenuItem("Cut");
edit.getItems().addAll(copy,paste,cut);
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(copy,cut,paste);
pane.setContextMenu(contextMenu);
pane.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
if (event.isSecondaryButtonDown()) {
System.out.println("asasasa");
contextMenu.show(pane, event.getScreenX(), event.getScreenY());
} else {
contextMenu.hide();
}
});

Related

New interface for each element of menu bar in java

I have created a menu bar where i have 5 items:
Now what i want to do is, for each items, when i click one of them, an interface dedicated for that item will be open under the menu bar in java, as shown in this example:
How can i do this in java? Thank you so much for your helps.
Here is my java code:
public class MainView extends Div {
public MainView() {
MenuBar menuBar = new MenuBar();
Text selected = new Text("");
ComponentEventListener<ClickEvent<MenuItem>> listener = e -> selected
.setText(e.getSource().getText());
Div message = new Div(new Text("Clicked item: "), selected);
menuBar.addItem("Administrateur", listener);
menuBar.addItem("Utilisateur", listener);
MenuItem share = menuBar.addItem("Article");
MenuItem move = menuBar.addItem("Fournisseur");
menuBar.addItem("Catégorie", listener);
add(menuBar, message);
}
}
Create an interface for each element of menu bar in java.

Cannot set a MouseEvent for MenuItem to trigger a ContextMenu. What would be an alternative solution?

Given the EventHandler code:
EventHandler<MouseEvent> MEvent = new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent arg0) {
if (arg0.getButton() == MouseButton.PRIMARY)
System.out.println("FIRE LEFT MB");
}
else if (arg0.getButton() == MouseButton.SECONDARY) {
System.out.println("FIRE RIGHT MB");
}
}
};
Unfortunately, when doing a setOnAction for a menuItem through gets()
MenuBar().getMenus().get(a).getItems().get(b).addEventHandler(MouseEvent.MOUSE_CLICKED, MEvent);
It will never fire. Apparently this has something to do with specific implementation of MenuItem. Is there anything that I can wrap the MenuItem in so that it can take a MouseEvent and it can still be added in Menus?
Is it possible to use ActionEvent with more specificity?
Perhaps a filter is the way to go?
If you don't want to check which mouse button was pressed, you can use simply setOnAction.
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("Menu");
MenuItem menuItem = new MenuItem("Click here");
menu.getItems().add(menuItem);
menuBar.getMenus().add(menu);
menuItem.setOnAction(e -> System.out.println("On Action"));
If you want a real mouse handler to be attached, you can try to make a CustomMenuItem with a Label as graphic, and then you can attach any listener on that Label:
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("Menu");
Label customMenuLabel = new Label("Custom menu item");
CustomMenuItem menuItem = new CustomMenuItem(customMenuLabel);
customMenuLabel.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
if (event.getButton() == MouseButton.PRIMARY)
System.out.println("FIRE LEFT MB");
else if (event.getButton() == MouseButton.SECONDARY)
System.out.println("FIRE RIGHT MB");
});
menu.getItems().add(menuItem);
menuBar.getMenus().add(menu);

JavaFX MenuItem does not react on MouseEvent.CLICKED

I am writting a little desktop application with a TreeView according to the Oracle-Example from here: https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm.
From a MenuItem action of a ContextMenu, I would like to fire an event which shall create a new TreeItem below the item where I opened the ContextMenu from.
For MenuItem, it is possible to use the setOnAction(EventHandler<ActionEvent> event) method, but I only want to fire the action from a left mouse-click.
First, it is not possible to add an EventHandler to a MenuItem although it provides the method addEventHandler(EventType type, EventHandler<EventType> handler) with the event-type MouseEvent.ANY (or anything else). The handle-method of the event-handler is not called.
Second, i can use a workarround by adding a Label to a MenuItem by menuItem.setGraphic(label) and add an EventHandler to the label. This one works although MouseEvent.MOUSE_CLICKED is not called by an EventHandler's handle-method on a Label.
Is this "normal" behaviour? I understand that a label does not react on a click-event, but I do not understand why it is not possible to register a separate EventHandler or EventFilter on a MenuItem.
ContextMenu uses a MenuItemContainer, which is a
Container responsible for laying out a single row in the menu - in other
words, this contains and lays out a single MenuItem, regardless of it's
specific subtype.
Fur this purpose it seems to create new Nodes representing the MenuItem. So any EventHandlers added to the MenuItem will not be called.
To make it work as you intended, you can use a CustomMenuItem and add the according EventHandler to its content:
public class ContextMenuCell extends TreeCell<String> {
private ContextMenu menu;
public ContextMenuCell() {
Label lbl = new Label("Add item");
MenuItem menuItem = new CustomMenuItem(lbl);
lbl.setOnMouseClicked(evt -> {
if (evt.getButton() != MouseButton.PRIMARY) {
return;
}
TreeItem treeItem =
new TreeItem<String>("New item");
if (getTreeItem().isLeaf()) {
getTreeItem().getParent().getChildren().add(getIndex(), treeItem);
} else {
getTreeItem().getChildren().add(0, treeItem);
}
});
menu = new ContextMenu(menuItem);
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(item);
setGraphic(getTreeItem().getGraphic());
setContextMenu(menu);
}
}
}
Menu and MenuItem are not Nodes, so they will not handle mouse clicks since they are not displayed on the screen. A workaround is to set a graphics object (Node) to the MenuItem and add the listener to this Node. Works also for other menus like CheckMenuItem etc.:
public class RunJavaFX extends Application {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
//the label will be our graphics object (Node)
Label l = new Label("Your Menu Text");
l.setTextFill(Color.BLACK); //set black since default CSS Style sets it to background color of the Menu
//add either over addEventFilter or addEventHandler
l.addEventFilter(MouseEvent.MOUSE_PRESSED, ev -> {
if (ev.getButton() == MouseButton.SECONDARY) {
System.out.println("RightClick: " + ev.getSource() + System.nanoTime());
} else {
System.out.println("Not Right Click: " + ev.getSource() + System.nanoTime());
}
ev.consume(); //optional
});
//create the MenuItem with an empty text and set the label l as graphics object
MenuItem mI = new MenuItem("", l);
//create the dummy menu and MenuBar for the example
Menu m = new Menu("Menu");
m.getItems().add(mI);
MenuBar mB = new MenuBar(m);
//create the dummy scene for the example
Scene scene = new Scene(mB);
primaryStage.setScene(scene);
primaryStage.show();
}
}

the menu items in menubar is not active and cannot be selected or clicked

I've got a problem. I'm writting a web application in the gwt. i wanna get a menubar this is my code:
Menu[] menus = new Menu[2];
MenuBar plikMenu = new MenuBar();
Menu menuPlik = new Menu();
MenuItem itemZmianaHasla = new MenuItem("Zmiana hasła...");
//MenuItem itemDodajUzytkownika = new MenuItem("Dodaj użytkownika...");
MenuItem itemDodajUzytkownika = new MenuItem("Dodaj użytkownika...");
itemDodajUzytkownika.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
public void onClick(final MenuItemClickEvent event) {
DodajUzytkownika uzytkownik = new DodajUzytkownika();
uzytkownik.center();
uzytkownik.show();
}
});
MenuItem itemUsunUzytkownika = new MenuItem("Usuń użytkownika");
MenuItem itemDodajGrupe = new MenuItem("Dodaj grupę...");
MenuItem itemZarzadzanieSesjami = new MenuItem("Zarządzanie sesjami");
MenuItemSeparator separator = new MenuItemSeparator();
MenuItem itemZarzadzanieLicencjami = new MenuItem("Zarządzanie licencjami");
MenuItem itemRaporty = new MenuItem("Raporty");
MenuItem itemBackupOracle = new MenuItem("Backup Oracle...");
MenuItem itemPrzywracanieOracle = new MenuItem("Przywracanie Oracle...");
MenuItem itemEksportLogiczny = new MenuItem("Eksport logiczny");
MenuItem itemWyjscie = new MenuItem("Wyjście");
menuPlik.setItems(itemZmianaHasla, itemDodajUzytkownika, itemUsunUzytkownika,itemDodajGrupe,itemZarzadzanieSesjami,separator, itemZarzadzanieLicencjami,itemRaporty,separator, itemBackupOracle,itemPrzywracanieOracle,itemEksportLogiczny,itemWyjscie);
menuPlik.setTitle("Plik");
menuPlik.setWidth(100);
menus[0] = menuPlik;
Menu menuPomoc = new Menu();
menuPomoc.setShowShadow(true);
menuPomoc.setShadowDepth(10);
MenuItem itemLicencja = new MenuItem("Licencja...");
MenuItem itemPodrecznikAdministratora = new MenuItem("Podręcznik administratora...");
MenuItem itemOProgramie = new MenuItem("O programie...");
menuPomoc.setItems(itemLicencja, itemPodrecznikAdministratora, itemOProgramie);
menuPomoc.setTitle("Pomoc");
menuPomoc.setWidth(80);
menus[1] = menuPomoc;
plikMenu.addMenus(menus, 0);
plikMenu.setWidth(80);
plikMenu.enable();
dialogHTopPanel.addMember(plikMenu);
i use a smartgwt components. the menubar with items is visible, but if I wanna click one of them, nothing happend. even the item is not highlighted.
Does anyone knows what I do wrong?
Thank you for any help
You need to specify the events that handle the click action of the menu.
For example if you want to add an action to one menu item you should do this as follows
MenuItem itemRaporty = new MenuItem("Raporty");
itemRaporty.addClickHandler(new ClickHandler() {
public void onClick(final MenuItemClickEvent event) {
//processClickEvent();
}
});

How to get MenuItems from ContributionItems within MenuManager?

I have a MenuManager that is filled with Actions, is it possible to access the corresponding MenuItem for the action (ContributionItem) ?
MenuManager menuManager = new MenuManager("#PopupMenu", "contextMenu");
menuManager.add(IAction1...);
menuManager.add(IAction2...);
Menu menu = menuManager.createContextMenu(myTreeVvewer.getControl());
myTreeVvewer.getControl().setMenu(menu);
myTreeVvewer.getTree().addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
//Iterate menu items of menu and set something...
}
}
It would probably be easier to use the option MenuManager#.setRemoveAllWhenShown(true); and then dynamically add the menu items in a IMenuListener.

Categories

Resources