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.
Related
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.
I have a pretty simple class that basically is just an AppLayout with some Tab.
Now my issue. I am not able to find a smart way to display different contents for the Tabs-class. Is there any interface or something that can be called to differ the content for the Tab?
class MainAppView extends AppLayout {
public MainAppView()
{
createDrawerAndAddToAppView();
}
void createDrawerAndAddToAppView()
{
Tabs tabs = createTabsForDrawer();
tabs.setOrientation(Tabs.Orientation.VERTICAL);
addToDrawer(tabs);
H1 a = new H1("Test"); // Is displayed as content for every Tab
tabs.addSelectedChangeListener(selectedChangeEvent ->
/**
* How to get the specific content of a Tab here?
*/
//selectedChangeEvent.getSelectedTab(). //getContent() and put in super.setContent()?
super.setContent(a)); // Displays 'Test' as content for every Tab
// The Listener shall display the specific content of the getSelectedTab()
}
private Tabs createTabsForDrawer()
{
return new Tabs(
new Tab("Home"),
new Tab("Dummy"),
new Tab("Test"));
}
}
Here is one example, using a map to keep track of which content belongs to which tab. In reality your tab content would be more complicated, and maybe be created in it's own method.
#Route
public class TabTest extends VerticalLayout {
private Map<Tab, Component> tabComponentMap = new LinkedHashMap<>();
public TabTest() {
Tabs tabs = createTabs();
Div contentContainer = new Div();
add(tabs, contentContainer);
tabs.addSelectedChangeListener(e -> {
contentContainer.removeAll();
contentContainer.add(tabComponentMap.get(e.getSelectedTab()));
});
// Set initial content
contentContainer.add(tabComponentMap.get(tabs.getSelectedTab()));
}
private Tabs createTabs() {
tabComponentMap.put(new Tab("Show some text"), new H1("This is the text tab"));
tabComponentMap.put(new Tab("Show a Combo Box"), new ComboBox<String>());
tabComponentMap.put(new Tab("Show a button"), new Button("Click me and nothing happens"));
return new Tabs(tabComponentMap.keySet().toArray(new Tab[]{}));
}
}
You can do something similar with routes also, but then you would probably want your containing component to be a RouterLayout. Also this requires a bit more logic if you want to automatically select the correct tab after navigating from somewhere else.
#Route
public class TabTest extends VerticalLayout implements RouterLayout {
private Map<Tab, String> tabToUrlMap = new LinkedHashMap<>();
private Div contentContainer = new Div();
public TabTest() {
Tabs tabs = createTabs();
Div contentContainer = new Div();
contentContainer.setSizeFull();
add(tabs, contentContainer);
tabs.addSelectedChangeListener(e ->
UI.getCurrent().navigate(tabToUrlMap.get(e.getSelectedTab())));
}
private Tabs createTabs() {
RouteConfiguration routeConfiguration = RouteConfiguration.forApplicationScope();
tabToUrlMap.put(new Tab("View 1"), routeConfiguration.getUrl(TestView1.class));
tabToUrlMap.put(new Tab("View 2"), routeConfiguration.getUrl(TestView2.class));
tabToUrlMap.put(new Tab("View 3"), routeConfiguration.getUrl(TestView3.class));
return new Tabs(tabToUrlMap.keySet().toArray(new Tab[]{}));
}
#Override
public void showRouterLayoutContent(HasElement content) {
getElement().appendChild(content.getElement());
}
}
And an example view
#Route(layout = TabTest.class)
public class TestView3 extends VerticalLayout {
public TestView3() {
add("View 3");
}
}
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();
}
});
I don't know how to add right-mouse click listener on a TreeViewer (JFace) item?
This is my source I create a class treeview extends from viewpart :
/*
* Creates the tree.
*
* #return the tree object and create part control
*/
private TreeObject CreateTree() {
TreeParent root = new TreeParent("");
DemoTreeModel ctModel = new DemoTreeModel();
List<String> listType = ctModel.getType();
for (String type : listType) {
TreeParent parentNode1 = new TreeParent(type);
parentNode1.setLevel(1);
List<String> listMachine = ctModel.getName(type);
if (listMachine != null) {
for (String machine : listMachine) {
TreeParent childNode1 = new TreeParent(machine);
childNode1.setLevel(2);
parentNode1.addChild(childNode1);
List<String> listVersion = ctModel.getVersion(machine);
if (listVersion != null) {
for (String ver : listVersion) {
TreeObject version = new TreeObject(ver);
version.setLevel(3);
version.setData(ver);
childNode1.addChild(version);
}
}
}
}
root.addChild(parentNode1);
}
return root;
}
You can use something like this:
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager menuManager) {
... add items to menu manager
}
});
Control tree = treeViewer.getControl();
Menu menu = menuMgr.createContextMenu(tree);
tree.setMenu(menu);
This is using an IMenuListener which lets you add different menu items each time the menu is displayed, so you can adapt to what is selected.
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();
}
});