nifty_gui_popup_menu.adoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. = Nifty GUI: Create a PopUp Menu
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :relfileprefix: ../../
  6. :imagesdir: ../..
  7. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  8. Even though you create and populate the popup menu in Java, you still need a “placeholder in your XML file.
  9. The popup element needs to be placed _outside_ of any screen!
  10. [source,xml]
  11. ----
  12. <useControls filename="nifty-default-controls.xml"/>
  13. ...
  14. <popup id="niftyPopupMenu" childLayout="absolute-inside"
  15. controller="ControllerOfYourChoice" width="10%">
  16. <interact onClick="closePopup()" onSecondaryClick="closePopup()" onTertiaryClick="closePopup()" />
  17. <control id="#menu" name="niftyMenu" />
  18. </popup>
  19. ...
  20. ----
  21. A brief explanation of some the attributes above:
  22. * The popup id is used within your Java code so that nifty knows which popup placeholder to create.
  23. * The Controller tells Nifty which Java class handles MenuItemActivatedEvent.
  24. * 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)
  25. * The control id is used by the Java class to define a control type (i.e. Menu)
  26. The Java code within your defined ScreenController implementation:
  27. [source,java]
  28. ----
  29. private Element popup;
  30. ...
  31. public void createMyPopupMenu(){
  32. popup = nifty.createPopup("niftyPopupMenu");
  33. Menu myMenu = popup.findNiftyControl("#menu", Menu.class);
  34. myMenu.setWidth(new SizeValue("100px")); // must be set
  35. myMenu.addMenuItem("Click me!", "menuItemIcon.png",
  36. new menuItem("menuItemid", "blah blah")); // menuItem is a custom class
  37. nifty.subscribe(
  38. nifty.getCurrentScreen(),
  39. myMenu.getId(),
  40. MenuItemActivatedEvent.class,
  41. new MenuItemActivatedEventSubscriber());
  42. }
  43. public void showMenu() { // the method to trigger the menu
  44. // If this is a menu that is going to be used many times, then
  45. // call this in your constructor rather than here
  46. createMyPopupMenu()
  47. // call the popup to screen of your choice:
  48. nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);
  49. }
  50. private class menuItem {
  51. public String id;
  52. public String name;
  53. public menuItem(String id, String name){
  54. this.id= id;
  55. this.name = name;
  56. }
  57. }
  58. ----
  59. * The createMyPopupMenu() method creates the menu with set width so that you can populate it.
  60. * The showMenu() method is called by something to trigger the menu (i.e. could be a Key or some other method).
  61. * Note: if you want to be able to access the popup via your id, use createPopupWithId(id, id) instead.
  62. To handle menu item events (i.e. calling a method when you click on a menu item), you register (subscribe) a EventTopicSubscriber&lt;MenuItemActivatedEvent&gt; class implementation to a nifty screen and element.
  63. [source,java]
  64. ----
  65. private class MenuItemActivatedEventSubscriber
  66. implements EventTopicSubscriber<MenuItemActivatedEvent> {
  67. @Override
  68. public void onEvent(final String id, final MenuItemActivatedEvent event) {
  69. menuItem item = (menuItem) event.getItem();
  70. if ("menuItemid".equals(item.id)) {
  71. //do something !!!
  72. }
  73. }
  74. };
  75. ----