Java: how to check if the JFrame menu bar is displayed in the system line or in the frame itself?

A JFramein (From Swing) allows you to set the menu bar (Instance MenuBarwith JFrame.setMenuBar(mb);). This menu bar may appear in different places depending on the system in which it is running. If the operating system in which the application is running has a menu bar at the top of the screen, this menu bar set to JFrameusually appears in this menu bar. If this is not supported, the menu bar will be displayed at the top of the frame itself.

In the example below you can see different types of behavior in different systems:

MenuBar on WindowsMenuBar on Mac OS X

This is an example of the code that I use to customize 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);

: , ( )? , . , , , , ?

+4
2

, apple.laf.useScreenMenuBar Mac OS, JMenuBar , Mac . , , , , . - :

if (System.getProperty("os.name").startsWith("Mac OS X")) {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("apple.awt.graphics.UseQuartz", "true");
    // etc.
}
+4

, ?

, , .

, , . :

Point location =  menuBar.getLocatonOnScreen();

, , .

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

, ? , .

+3

All Articles