123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- = Nifty GUI: Create a PopUp Menu
- :author:
- :revnumber:
- :revdate: 2016/03/17 20:48
- :relfileprefix: ../../
- :imagesdir: ../..
- ifdef::env-github,env-browser[:outfilesuffix: .adoc]
- Even though you create and populate the popup menu in Java, you still need a “placeholder in your XML file.
- The popup element needs to be placed _outside_ of any screen!
- [source,xml]
- ----
- <useControls filename="nifty-default-controls.xml"/>
- ...
- <popup id="niftyPopupMenu" childLayout="absolute-inside"
- controller="ControllerOfYourChoice" width="10%">
- <interact onClick="closePopup()" onSecondaryClick="closePopup()" onTertiaryClick="closePopup()" />
- <control id="#menu" name="niftyMenu" />
- </popup>
- ...
- ----
- A brief explanation of some the attributes above:
- * The popup id is used within your Java code so that nifty knows which popup placeholder to create.
- * The Controller tells Nifty which Java class handles MenuItemActivatedEvent.
- * The on(Secondary/Tertiary)Click tells Nifty to close the popup if the user clicks anywhere except on the menu items (in this example; you have to define the closePopup()-method yourself, in the screen controller)
- * The control id is used by the Java class to define a control type (i.e. Menu)
- The Java code within your defined ScreenController implementation:
- [source,java]
- ----
- private Element popup;
- ...
- public void createMyPopupMenu(){
- popup = nifty.createPopup("niftyPopupMenu");
- Menu myMenu = popup.findNiftyControl("#menu", Menu.class);
- myMenu.setWidth(new SizeValue("100px")); // must be set
- myMenu.addMenuItem("Click me!", "menuItemIcon.png",
- new menuItem("menuItemid", "blah blah")); // menuItem is a custom class
- nifty.subscribe(
- nifty.getCurrentScreen(),
- myMenu.getId(),
- MenuItemActivatedEvent.class,
- new MenuItemActivatedEventSubscriber());
- }
- public void showMenu() { // the method to trigger the menu
- // If this is a menu that is going to be used many times, then
- // call this in your constructor rather than here
- createMyPopupMenu()
- // call the popup to screen of your choice:
- nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);
- }
- private class menuItem {
- public String id;
- public String name;
- public menuItem(String id, String name){
- this.id= id;
- this.name = name;
- }
- }
- ----
- * The createMyPopupMenu() method creates the menu with set width so that you can populate it.
- * The showMenu() method is called by something to trigger the menu (i.e. could be a Key or some other method).
- * Note: if you want to be able to access the popup via your id, use createPopupWithId(id, id) instead.
- To handle menu item events (i.e. calling a method when you click on a menu item), you register (subscribe) a EventTopicSubscriber<MenuItemActivatedEvent> class implementation to a nifty screen and element.
- [source,java]
- ----
- private class MenuItemActivatedEventSubscriber
- implements EventTopicSubscriber<MenuItemActivatedEvent> {
-
- @Override
- public void onEvent(final String id, final MenuItemActivatedEvent event) {
- menuItem item = (menuItem) event.getItem();
- if ("menuItemid".equals(item.id)) {
- //do something !!!
- }
- }
- };
- ----
|