macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Write Twice, Run Anywhere
Pages: 1, 2, 3

Setting Up the Two Menu Bars

Let's start by setting up the basic framework.



To help you keep track of where we're heading, when we're finished, the directory structure will look like this.

Screen shot.
Directory structure for basic framework.

Create the first version of the MacJMenuBar. This just puts appropriately labeled JMenus into the JMenuBar.

package MacGUI;

import javax.swing.JMenuBar;
import javax.swing.JMenu;

public class MacJMenuBar extends JMenuBar{
  public MacJMenuBar(){
    add(new JMenu("File"));
    add(new JMenu("Edit"));
    add(new JMenu("Format"));
    add(new JMenu("Window"));
    add(new JMenu("Help"));
  }
}

The code for the WindowsJMenuBar is the same except for the labels for some of the JMenus.

package WindowsGUI;

import javax.swing.JMenuBar;
import javax.swing.JMenu;

public class WindowsJMenuBar extends JMenuBar{
  public WindowsJMenuBar(){
    add(new JMenu("File"));
    add(new JMenu("Edit"));
    add(new JMenu("View"));
    add(new JMenu("Insert"));
    add(new JMenu("Format"));
    add(new JMenu("Help"));
  }
}

Choosing the Right Menu Bar

Now we'll create the MainFrame class. This is a JFrame that contains the main() method for this application and chooses the correct JMenuBar for the current operating system. Other than add a name to the title bar and adjust the size of the JFrame, the constructor includes the key call to the method getCorrectMenuBar().

In this method we check to see if we are running on a Mac by checking the value of the System property mrj.version. Note that this is the method recommended by Apple. This value is only set on the Mac so if it isn't null, we know our application is running on a Mac and can create the Mac version of the JMenuBar. Otherwise we'll create the Windows version. Incidentally, the initial version of Java shipping on Jaguar has this property set to 3.3. Here's the first version of the MainFrame class.

import javax.swing.JMenuBar;
import javax.swing.JFrame;
import MacGUI.MacJMenuBar;
import WindowsGUI.WindowsJMenuBar;

public class MainFrame extends JFrame{

  public MainFrame(){
    super("Write Twice Run Anywhere");
    setJMenuBar(getCorrectMenuBar());
    setSize(400,400);
    setVisible(true);
  }
  private JMenuBar getCorrectMenuBar(){
    if (System.getProperty("mrj.version")!=null){ 
      return new MacJMenuBar();
    } else return new WindowsJMenuBar();
  }

  public static void main(String [] args){
    new MainFrame();
  }
}

Compile and run this program and you'll see this menu bar.

Screen shot.
Close, but no prize. The menu bar still isn't in the right place.

Arrrrgh, that's not right. The menu bar isn't in the right place. It's inside of the JFrame not up in the system menu bar spot where it belongs. We need to set a system property. We can do that when we run the application, but let's go ahead a put it in the code. If we discover that the application is running on a Mac, we'll set the system property to display the JMenuBar in the correct location. Here's the revised version of the getCorrectMenuBar() method.

private JMenuBar getCorrectMenuBar(){
   if (System.getProperty("mrj.version")!=null){
      
System.setProperty("com.apple.macos.useScreenMenuBar","true");
      return new MacJMenuBar();
   } else return new WindowsJMenuBar();
}

With this simple addition, as you can see below, the menu bar is moved when the application runs on a Mac. The menu bar stays at the top of the JFrame when the application runs on other platforms.

Screen shot.
Ahhh, that's better. Now things look right!


Pages: 1, 2, 3

Next Pagearrow