How to add menu items to your Dock menu in Mac OS X Java applications

If you want to add menu items to your Dock menu in Mac OS X Java applications, I can confirm that this approach works:

// create the menu items
val foo = new java.awt.MenuItem("Foo")
val bar = new java.awt.MenuItem("Bar")

// create the menu, and add the menu items
val menu = new java.awt.PopupMenu
menu.add(foo)
menu.add(bar)

// set the dock menu
val app = com.apple.eawt.Application.getApplication
app.setDockMenu(menu)

I wrote that in Scala, but it’s close enough to Java that you get the idea. This code makes the menu items “Foo” and “Bar” show up when you right-click your Mac/Java application’s icon in the Dock. Of course you still need to add listeners to your menu items to get them to do something useful, but as far as adding new menu items to your app’s Dock menu, I can confirm that this works.

FWIW, on Mac OS X 10.9, these new menu items appear above the standard Quit, Hide, and Show All Windows Dock menu items. At first I thought I may have to call getDockMenu, then add my items to that menu, then call setDockMenu, but that wasn’t necessary.

It’s also worth nothing that the setDockMenu method expects a PopupMenu, not a JPopupMenu.