Java example: JMenuBar + KeyStroke + AbstractAction

As a brief note, here’s some source code that I used to create a JMenuBar in a Java application. First, I defined some fields in my main class:

private static final KeyStroke fileOpenKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.META_MASK);
private Action fileOpenAction;
private JMenuBar menuBar;

Later in the same class I defined this method:

macOS: How to put keyboard focus on the Mac menubar

For a long time I've read that you could use your Apple/Mac keyboard to access the menubar on MacOS systems using the [Control][F2] keystroke. That never actually worked, so I logically assumed that everyone else was insane.

Well, it turns out that what really happened is that silly me was using Apple keyboards ... and as I was trying to figure this out again today, I looked over at that [fn] key on the keyboard, and thought, "No, surely I wouldn't have to press that too ..."

JFrame examples and tutorials (collection)

Today is organization day for me, and in an effort to organize everything I've ever written about a Java JFrame, here is a collection of links to all my Java JFrame examples and tutorials.

Java on MacOS: A default Mac menubar without a JFrame

Yesterday I ran into a pretty obscure situation in my Imagen application where I needed to show a modified Mac menubar, even though I didn't have a Java JFrame displayed at the time. The way Imagen works is that I show a JFrame, then hide it, and the application then waits for the user to do something with the application icon in the Mac Dock, typically dropping an image onto that icon.

Options to put your Java application name on the Mac menubar

If you don't do anything to your Java application on a Mac OS X system, your Java class name will appear in the Mac menubar. Of course, this isn't a good thing. There are a couple of things you can do to get the name of your Java application on the Mac menu bar, and I'll share all of the ways I know how to do this. I've listed these techniques in order here from "easiest" to "best".

A Mac Java ApplicationAdapter class example

Before I get too far away from all of the Java/Swing/Mac GUI code I've been working on lately, I thought I'd share this Mac Java ApplicationAdapter class implementation.

This class is an example implementation of Apple's ApplicationAdapter class, which itself is a stub implementation of their own ApplicationListener interface. If you want to write Java GUI code on Mac OS X, it's important to learn about the ApplicationAdapter class, so you can handle the About, Preferences, and Quit events properly.