Home » Java » Desktop Java » PopupMenu in JavaFX 2

About Pedro Duque Vieira

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()
    .onAction(new EventHandler() {
    public void handle(ActionEvent actionEvent) {
      // some code that gets called when the user clicks the menu item

    .onAction(new EventHandler() {
    public void handle(ActionEvent actionEvent) {
      // Some code that will get called when the user clicks the menu item
  • 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()))
  • 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)
  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 our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....



Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns