Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
add comment

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

share|improve this answer
    
Have you confirmed that libjava-gnome supports libdbusmenu at all? Unity is not Gnome, after all. Also, I think switching to another GUI toolkit just in order to use a dbus service to be fairly radical. –  Jo-Erlend Schinstad Jan 29 '12 at 15:36
    
Yes, I've confirmed :). –  Marcos Roriz Junior Feb 3 '12 at 3:47
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
-1 for not a real answer and for asking the op to implement the dbus stuff. –  Marcos Roriz Junior Jan 29 '12 at 14:25
1  
Not a real answer? How can you say that? I answered precisely what was asked. The OP did not ask how to port a Swing application to GTK. –  Jo-Erlend Schinstad Jan 29 '12 at 15:34
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.