Pedro Duque Vieira

About Pedro Duque Vieira

Pedro DV is a Software Engineer and self taught designer with 7 years experience in user interface/front end development. He's also the co-creator of Modellus, an hobby project.

PopupMenu in JavaFX 2

Creating Popup Menus

To create a Popupmenu in JavaFX you can use the ContextMenu class.

You add MenuItems to it and can also create visual separators using SeparatorMenuItem.

In the example below I’ve opted to subclass ContextMenu and add the MenuItems on its constructor.

public class AnimationPopupMenu extends ContextMenu{
public AnimationPopupMenu()
{
(...)
  getItems().addAll(
    MenuItemBuilder.create()
    .text(ADD_PARTICLE)
    .graphic(createIcon(...))
    .onAction(new EventHandler() {
    @Override
    public void handle(ActionEvent actionEvent) {
      // some code that gets called when the user clicks the menu item
    }
    })
    .build(),

(...)
    SeparatorMenuItemBuilder.create().build(),
    MenuItemBuilder.create()
    .text(ADD_DISTANCE_MEASURER)
    .onAction(new EventHandler() {
    @Override
    public void handle(ActionEvent actionEvent) {
      // Some code that will get called when the user clicks the menu item
    }
  })
  .graphic(createIcon(...))
  .build(),
(...)
  );
}
  • Line 5: I get the Collection of children of the ContextMenu and call addAll to add the MenuItems;
  • Line 6: Uses the MenuItem builder do create a MenuItem;
  • Line 7: Passes in the text of the menu item. Variable ADD_PARTICLE is equal to “Add Particle”;
  • Line 8: Calls graphic which receives the menu item icon returned by createIcon:
    ImageView createIcon(URL iconURL)
        {
            return ImageViewBuilder.create()
                                   .image(new Image(iconURL.toString()))
                                   .build();
        }
    
  • Line 9: onAction receives the event handler which will be called when the user clicks the menu item;
  • Line15: Finally the MenuItem gets created by executing build() on the MenuItemBuilder class;
  • Line18: Creates The Separator which you can see on the figure on the start of this post. It’s the dotted line between “Add Origin” and “Add Distance Measurer”;
  • The other lines of code just repeat the same process to create the rest of the menu items.



Using JavaFX Popup Menus inside JFXPanel
If your embeding a JavaFX scene in a Swing app you’ll have to do some extra steps manually, if you don’t there won’t be hover animations on the popup menu and it won’t get dismissed automatically when the user clicks outside of it. There is a fix targeted at JavaFX 3.0 for this – http://javafx-jira.kenai.com/browse/RT-14899
First you’ll have to request the focus on the javafx container so that the popup gets hover animations and when you click outside your app window it gets dismissed. In my case I pass a reference to the javafx swing container on the construtor of the popup menu, then I’ve overwritten the show method of ContextMenu so as to request the focus on the swing container before actually showing the popup:

public void show(Node anchor, MouseEvent event)
{
  wrapper.requestFocusInWindow();
  super.show(anchor, event.getScreenX(), event.getScreenY());
}

And lastly you’ll have to also dismiss the popup when the user clicks inside the javafx scene but outside of the popup by calling hide().
I almost forgot.. thanks to Martin Sladecek (Oracle JavaFX team) for giving me some pointers.

Reference: PopupMenu in JavaFX 2 from our JCG partner Pedro Duque Vieira at the Pixel Duke blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


− one = 5



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close