How do I make my java app compatible with the unity global menu?

I like to develop in Java but also want compatibility with the global menu? Is there a way? Please help.


Solution 1:

There is a library called java-gnome (a java gtk wrapper) that allows your apps to behave like all of the native apps. It provides all the glory to them: native widgets, global menu, etc.

The library is available on the ubuntu repositories:

  • Library: libjava-gnome-java Install libjava-gnome-java
  • Examples: libjava-gnome-java-doc Install libjava-gnome-java

The goal of the library is to develop a rich Java + Gnome experience. Beware, that if you use this library your app will lose the multiplatform feature of Java (since it'll be tied to java-gnome).

Here is a sample app (that comes with java-gnome) displaying the menu integration:

Example App

Solution 2:

Try Ayatana. You may find instructions here: http://hanynowsky.wordpress.com/2012/05/12/integration-of-java-swing-applications-with-unity-global-menu-in-ubuntu-12-04/

All you have to do is to import the Java Ayatana Library and call it in your main JFrame class. This actually implies the addition of the following couple of lines in your code (as well as importing Ayatana classes):

if (AyatanaDesktop.isSupported())
      ApplicationMenu.tryInstall(mainFrame);

Here is an example:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import org.java.ayatana.ApplicationMenu;
import org.java.ayatana.AyatanaDesktop;
public class MyAppWithUnityMenu {
public MyAppWithUnityMenu () {
    JFrame mainFrame = new JFrame("This app integrates in Unity menu bar");
// set up mainFrame, by adding components, etc.
    JPanel panel = new JPanel();
    panel.add(new JLabel("This is a sample application for testing menu integration in Unity. Enjoy!"));
    mainFrame.getContentPane().add(panel);     
    mainFrame.addWindowListener ( new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
    });
 // set up the menu bar, by adding menus, etc.
  JMenuBar menuBar = new JMenuBar();
  JMenu file = new JMenu("File");
  file.add(new JMenuItem("Open"));
  file.add(new JMenuItem("Save"));
  file.add(new JMenuItem("Close"));
  JMenu edit = new JMenu("Edit");
  edit.add(new JMenuItem("Copy"));
  edit.add(new JMenuItem("Cut"));
  edit.add(new JMenuItem("Paste"));
  JMenu help = new JMenu("Help");
  help.add(new JMenuItem("Help topics"));
  help.add(new JMenuItem("About"));
  menuBar.add(file);
  menuBar.add(edit);
  menuBar.add(help);
  menuBar.setVisible(true);
  mainFrame.setJMenuBar(menuBar);
  mainFrame.pack();
  mainFrame.setVisible(true);
   // Java Ayatana Call
  if (AyatanaDesktop.isSupported()) { 
      ApplicationMenu.tryInstall(mainFrame);
  }
}
public static void main(String[] args) {
    new MyAppWithUnityMenu();
}
}

And the most important thing is that your application is still cross-platform. I have tested the above example in both Ubuntu 12.04 LTS and Windows XP.

Solution 3:

You can use a ppa: ppa:danjaredg/jayatana

http://www.webupd8.org/2014/02/get-unity-global-menu-hud-support-for.html