4

A JFrame in (From Swing) allows you to set a menu bar (Instance of MenuBar using JFrame.setMenuBar(mb);). This menu bar can appear in different locations depending on the system it's running on. If the operating system the appliation is running on has a menu bar on top of the screen, this menu bar set in the JFrame usually appears in this menu bar. If this is not supported, the menu bar will be shown in the top of the frame itself.

You can see the different behaviours on different systems in the example bellow:

MenuBar on Windows MenuBar on Mac OS X

This is an example of the code I use to set up the menu bar:

// Initialize a menu bar
MenuBar mb = new MenuBar();

// Initialize the menu and some menu items, add these to the menu bar
Menu m = new Menu("Menu 1");
MenuItem mi1 = new MenuItem("Menu Item 1");
MenuItem mi2 = new MenuItem("Menu Item 2");
MenuItem mi3 = new MenuItem("Menu Item 3");
m.add(mi1);
m.add(mi2);
m.addSeparator();
m.add(mi3);
mb.add(m);

// Set the menu bar
setMenuBar(mb);

My question is: How do I check whether the menu bar will be shown in the frame itself or in the system's menu bar (if supported)? It would be great if there's any possible way to check this without initializing and defining a menu bar. Maybe this is impossible, if that is the case, is it possible to check for this same problem after a menu bar has been defined?

Tim Visée
  • 2,988
  • 4
  • 45
  • 55

2 Answers2

4

As shown here, apple.laf.useScreenMenuBar specifies a Mac OS feature that displays an existing JMenuBar atop the screen, where a Mac user expects to see it. Although it should be irrelevant to your application, the only time the menu bar is displayed there is when you put it there. A suitable cross-platform predicate might look like this:

if (System.getProperty("os.name").startsWith("Mac OS X")) {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("apple.awt.graphics.UseQuartz", "true");
    // etc.
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, this works fine. I've a question though, what is `apple.awt.graphics.UseQuartz` used for? I took a look on this page: `https://developer.apple.com/library/mac/documentation/java/conceptual/java14development/07-nativeplatformintegration/nativeplatformintegration.html` but it's still not clear to me. I changed the value both to `true` and `false` but I didn't notice any difference in the appearance of the menu. – Tim Visée Nov 23 '13 at 18:08
  • It's just an example of another Mac specific property; it may not be relevant to your application. I use it to improve glyph rendering at large point sizes. – trashgod Nov 23 '13 at 18:21
  • 1
    @TimVisee: See also [*Java Runtime System Properties*](https://developer.apple.com/library/mac/documentation/java/Reference/Java_PropertiesRef/Articles/JavaSystemProperties.html). – trashgod Nov 24 '13 at 00:33
  • @trashgod Thanks! This should help a lot. – Tim Visée Nov 24 '13 at 00:36
  • @trashgod The point is, that it's not really an answer to my question (in my opinion). The point is that this answer shows me a way to force the system's menu bar to be used on Mac OS X systems. This is not a way to check whether the use of a system's menu bar is supported, or am I wrong? – Tim Visée Nov 24 '13 at 00:41
  • The choice to accept an answer must be yours. To the extent that this is an [XY question](http://meta.stackexchange.com/q/66377/163188), I would accept @camickr's answer favoring cross-platform functionality. If you have a compelling reason to distinguish environments, the property approach may be best. – trashgod Nov 24 '13 at 01:00
3

is it possible to check for this same problem after a menu bar has been defined?

I only use windows so I don't know how it works on other OS.

I don't think you can find this information out until the frame is visible. Then you should be able to use:

Point location =  menuBar.getLocatonOnScreen();

Then you can compare that location with the location of the frame to see if it is contained within the bounds of the frame.

if (frame.getBounds().contains(location))
    ....

The question is why do you need this information? Maybe we can suggest an alternate approach.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I like your way of thinking, this will work but in some cases it might fail. For example, if the window is at (0,0) this method will fail. I'm developing an application, with custom components and a lot of special features and functionality. I would like to use the top MenuBar of the system if this is available, if this isn't available I don't want this menu to show up, instead I would like to show up a few other components. This is just a basic description of my problem, but I think this clearly shows you what I would like to achieve. Btw, I've added example images to my question. – Tim Visée Nov 23 '13 at 16:04
  • 1
    +1 for suggesting a search for an alternate approach; it should't matter where the menu bar is displayed; more [here](http://stackoverflow.com/a/20165540/230513). – trashgod Nov 23 '13 at 17:31