M HYPE SPLASH
// general

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

By Emily Wilson

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

5 Answers

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:

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

2

Try Ayatana. You may find instructions here:

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.

0

You can use a ppa: ppa:danjaredg/jayatana

That is normally done at the toolkit level, for instance in GTK or Qt. So the intended way to do it would be to patch Swing, if that's the GUI toolkit you're using. However, all the Unity components communicate using DBus, so if you really wanted to, you could do it yourself by implementing the DBus API yourself. If you want to do this, you might want to have a look at libdbusmenu and see how it works.

2

Not a fix for you, but FYI: I filed Ubuntu bug #984916 to track the fact that Swing menus are not (semi-)automatically integrated into the global menu.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy