New interface for each element of menu bar in java - 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.

Related

Submenu for Context Menu in SWT Text

I want to have a pop-up menu with a simple submenu. On right-click on the SWT Text (commandText). What I want to achieve is this:
A -> D
E
F
B
C
So there should be Actions "D,E,F" under the Action "A". "B" and "C" are actions on top level, just like "A". My try is:
private void addCommandTextContextMenu() {
MenuManager popupMenu = new MenuManager("#PopupMenu");
popupMenu.setRemoveAllWhenShown(true);
popupMenu.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
Action aAction = new Action("A") {};
Action bAction = new Action("B") {};
Action cAction = new Action("C") {};
manager.add(aAction);
manager.add(bAction);
manager.add(cAction);
}
});
MenuManager subMenu = new MenuManager("#SubMenu");
subMenu.setRemoveAllWhenShown(true);
subMenu.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
Action dAction = new Action("D") {};
Action eAction = new Action("E") {};
Action fAction = new Action("F") {};
manager.add(dAction);
manager.add(eAction);
manager.add(fAction);
}
});
popupMenu.add(subMenu);
final Menu menu2 = popupMenu.createContextMenu(commandText);
commandText.setMenu(menu2);
}
I can only see A, B, C.
I try to add this pop-up menu for an eclipse plugin with Java only because I thought it should be easier than defining a menu in the plugin.xml with commands and handlers.
Just create the sub-menu and add the sub menu actions directy to the sub-menu:
public void menuAboutToShow(final IMenuManager manager) {
final Action bAction = new Action("B") {};
final Action cAction = new Action("C") {};
final Action dAction = new Action("D") {};
final Action eAction = new Action("E") {};
final Action fAction = new Action("F") {};
final MenuManager subMenu = new MenuManager("A");
subMenu.add(dAction);
subMenu.add(eAction);
subMenu.add(fAction);
manager.add(subMenu);
manager.add(bAction);
manager.add(cAction);
}
Add the sub-menu manager to the top level manager. The name of the sub-menu manager is used for the top level menu item.

adding context menu to pane in javafx

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();
}
});

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();
}
}

How to change text color on menu item

I'm using Java and SWT. I have added menu(on right click) to system tray. I want to change color of the text on menu items. Is it possible? I couldn't find any solution about this.
TrayItem item = new TrayItem(tray, SWT.NONE);
item.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(org.eclipse.swt.widgets.Event event) {
Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem item1 = new MenuItem(menu, SWT.NONE);
item1.setText("Settings"); // **i want to change color of the text**
}
}
No, this is not possible yet. There is a bug report here.
item1.setForeground(Color.red);
u mean this one??

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