Integration of Java Swing Applications with Unity Global Menu in Ubuntu 12.04

Java Swing Ayatana – Ubuntu Global Menu Support

Thanks to the new Project named Java Ayatana (Initiated by Jared Gonzales), Java Swing applications developers can now add support for Ubuntu Unity Global menu integration and Unity Launcher (Only one instance of Netbeans entry will be allowed in the Launcher and will persist despite logout or reboot).

All you have to do is import the Java Ayatana Library to your Java project CLASSPATH and call it in your Main JFrame object Class.

Java Ayatana Library in Classpath (Netbeans)

Java Ayatana Library in Classpath (Netbeans)

/**
* It is necessary for javax.swing.JFrame to
* include a menu bar javax.swing.JMenuBar object.
*/
import javax.swing. *;
 import org.java.ayatana.ApplicationMenu;

 public class Window extends JFrame {
public Window () {
/ / Setup window ...
// Check if Ayatana Desktop is supported
ApplicationMenu.tryInstall (this);
}
 }
  • Here is a JFrame example that you can use to test Java Ayatana:

package swingtest;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.java.ayatana.ApplicationMenu;

/**
* Java Ayatana: Java Swing Global Menu Support in Ubuntu
*
* @author Hanynowsky
*/
public class MenuBarExample extends JFrame {

/**
* Constructor
*/
public MenuBarExample() {

// Components
String s = "I am a basic example of Java Swing Menu Bar"
+ " that uses Java Ayatana Library: Java Swing Support for Unity "
+ "GLobal Menu in Ubuntu";
final JTextPane textarea = new JTextPane();
Font font = new Font("Liberation Mono", Font.PLAIN, 14);
textarea.setFont(font);
textarea.setText(s);
//textarea.
getContentPane().add(textarea);
setTitle("MenuBarExample");
setSize(500, 350);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu viewMenu = new JMenu("View");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(viewMenu);
JMenuItem newAction = new JMenuItem("New");
JMenuItem openAction = new JMenuItem("Open");
openAction.setEnabled(false);
JMenuItem exitAction = new JMenuItem("Exit");
JMenuItem cutAction = new JMenuItem("Cut");
cutAction.setEnabled(false);
JMenuItem copyAction = new JMenuItem("Copy");
copyAction.setEnabled(false);
JMenuItem pasteAction = new JMenuItem("Paste");
pasteAction.setEnabled(false);
JMenuItem toolbarsAction = new JMenuItem("toolbarsAction");
fileMenu.add(newAction);
fileMenu.add(openAction);
fileMenu.addSeparator();
fileMenu.add(exitAction);
editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
editMenu.addSeparator();
viewMenu.add(toolbarsAction);
exitAction.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
newAction.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked on the new action");
textarea.setText("What did you expect by clicking me! A new cake?");
}
});
}

/**
* Main Method
*
* @param args
*/
public static void main(String[] args) {
MenuBarExample mba = new MenuBarExample();
mba.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mba.setVisible(true);
// Java Ayatana Call
if (AyatanaDesktop.isSupported())
{ ApplicationMenu.tryInstall(mba);}
}
}

  • And the result is this:
Global Menu Support in Java Swing (Java Ayatana)

Global Menu Support in Java Swing (Java Ayatana)

And HUD:

HUD Integration

HUD Integration


NetBeans IDE 7.1.2 Support

: Global Menu & Unity Launcher

Let’s also note that the same library is used into a NBM plugin for Netbeans IDE, updated for version 7.1.2. You can get the plugin in Java Swing Ayatana google code repository.

Open your Netbeans IDE, then: Options / Plugins. And add the downloaded plugin and then install it (Restart NetBeans). You can also install it using the catalog XML file retrieved from : http://java-swing-ayatana.googlecode.com/files/netbeans-catalog.xml

After Installation, if the plugin does not work, Go to : /home/yourUserName/.local/share/applications/

and check if there is a file : netbeans-7.1.2.desktop

Open it with your text editor (ex: gedit). It should be similar to this.


[Desktop Entry]
Name=Netbeans
Exec=netbeans
Icon=netbeans
Categories=Application;Development;Java;IDE
StartupWMClass=netbeans-7.1.2
StartupNotify=true
Terminal=false
Type=Application

N.B: The property ‘Exec=netbeans‘ assumes that you are already able to run NetBeans in Terminal using the command : netbeans instead of the whole path to netbeans executable.
Finally, drag the same file to your Unity Launcher, if not yet done.

Netbeans Integrtion with Ubuntu Unity Global Menu & Launcher

Netbeans Integrtion with Ubuntu Unity Global Menu & Launcher

Netbeans Integrtion with Ubuntu Unity HUD

Netbeans Integrtion with Ubuntu Unity HUD

—————————————————

SOLUTION 2 :  JRE endorsed.

Today, you can set your default JRE (Java Runtime Environment) (Oracle JRE or OpenJDK JRE) to call Jayatana whenever the VM is run by  a swing application.

You no more need to import the Jayatana library into your Swing project classpath. Now, execute your swing application in ubuntu and it’s automatically integrated with Global Menus.

What to do?

Depending on your JDK of choice, here are the appropriate steps to follow:

OpenJDK JDK 7:

On Terminal enter, one comand by one:

Switch to your home directory:

cd ~/

Install the required packages

sudo apt-get install openjdk-7-jdk libunity-dev libxt-dev gcc make ant subversion

Update Java alternatives (set OpenJDK as default)

sudo update-java-alternatives -s java-1.7.0-openjdk-amd64

Use Subversion to checkout Jayatana source code (this will copy the library source code to your home folder).

svn checkout http://java-swing-ayatana.googlecode.com/svn/tags/1.2.4/jayatana

switch to the newly created folder (/jayatana)

cd ~/jayatana

Use ANT to unbuild the project if already built.

sudo ant deinstall

Use ANT to build and configurate the project

sudo ant install
sudo ant config

ORACLE JDK 7

sudo apt-get install openjdk-7-jdk libunity-dev libxt-dev gcc make ant subversion
svn checkout http://java-swing-ayatana.googlecode.com/svn/tags/1.2.4/jayatana
cd ~/jayatana
sudo ant deinstall
sudo ant install -Djava.home.7=/usr/lib/jvm/oraclejdk7
sudo ant config -Djava.home.7=/usr/lib/jvm/oraclejdk7

Where /usr/lib/jvm/oraclejdk7 is your JDK_7 home directory.

The following screenshot illustrates Intellij IDEA Global menu integration in Ubuntu 12.10 using SOLUTION 2.

intellij-idea

IntellijIDea111_Ubuntu

IntellijIDea111_Ubuntu

Semi-Automatic Installation

If you want to automate the Jayatana installation, you can use or download this script below as follows:
- Copy/Paste the script content in a file and name it : install-jayatana.sh
- Save the file in your Desktop folder.
- go to Terminal and type: sh ~/Desktop/install-jayatana.sh- Follow instructions.
- Later on, if you want to uninstall Jayatana, use the same script again.

#!/bin/bash

# ----------------------------------------------
# Author: hanynowsky@gmail.com
# Before executing this script make sure 
# you're in your home directory `cd $HOME`
#--------------------------------------------------

# -------------------------------------NOTICE------------
# APPLICABLE ONLY IN UBUNTU UNITY 
# This script installs Jayatana for your JDKs 
# It supposes you already have set your JAVA_HOME in ~/.bashrc
# You will be prompted to choose which JDK to set as Default 
# (choose the corresponding index).
# In case you already installed jayatana and want to re-install, 
# please uninstall then install again.
# ------------------------------------------------------

# -------------------------------------LICENSE--------------------
#Licensed under the GNU LGPL, Version 3.0 (the "License");
# you may not use this file except in compliance with the License
# ----------------------------------------------------------------

#JDKPATH=`echo $JAVA_HOME | sed -e 's/bash://g' | sed -e 's/Is a directory//g'`
#JDKPATH=eval "echo $JAVA_HOME"
JDKPATH=`echo $JAVA_HOME | grep [^*]`
USERHOME=`echo $HOME`
JAYATANADD="http://java-swing-ayatana.googlecode.com/svn/tags/1.2.4/jayatana"

# INSTALL 
INSTALLCOM () {
sudo apt-get -y install openjdk-7-jdk libunity-dev libxt-dev gcc make ant subversion 
sudo update-java-alternatives -s java-1.7.0-openjdk-amd64 
svn checkout $JAYATANADD 
sudo ant install -buildfile $USERHOME/jayatana/build.xml -v
sudo ant config -v 
sudo update-alternatives --config java 
sudo update-alternatives --config javac
sudo ant install -Djava.home.7=/usr/lib/jvm/java-7-openjdk-amd64 -f $USERHOME/jayatana/build.xml -v -l $USERHOME/Desktop/jayatana-stall.log
sudo ant config -Djava.home.7=$JDKPATH -f ~/jayatana/build.xml -v -l $USERHOME/Desktop/jayatana-stall.log
}

# UNINSTALL
UNINSTALLCOM (){
sudo ant deinstall -buildfile $USERHOME/jayatana/build.xml -v
sudo ant deconfig -buildfile $USERHOME/jayatana/build.xml -v
sudo ant deconfig -Djava.home.7=$JDKPATH -f $USERHOME/jayatana/build.xml -v -l $USERHOME/Desktop/jayatana-stall.log
}

# DELETE Jayatana Folder created by subversion checkout
UNDIR() {
read -p "Delete Jayatana Folder too? Enter ( y ) to delete or ( n ) otherwise : " choice
if [ "$choice" = "y" ]; then mv $HOME/jayatana $HOME/.local/share/Trash/files/
elif [ "$option" = "n" ]; then echo"Jayatana Folder is kept by $USER";
echo "you can delete it later : ~/jayatana";
else echo "Jayatana Folder is kept by $USER , you can delete it later : ~/jayatana";
fi
}

# COMMON
COMMON() {
echo "$JDKPATH : is your default JDK. Version: " 
echo `java -version`
}
eval COMMON;
read -p "Enter ( i ) to install or ( u ) to uninstall : " option
if [ "$option" = "i" ]; then eval INSTALLCOM
elif [ "$option" = "u" ]; then eval UNINSTALLCOM;eval UNDIR;
else echo "Aborted by $USER";
fi
exit 0;

DRAWBACKS

  • Netbeans IDE: You will lose launcher icon consistency (Unless you install jayatana netbeans plugin jayatana.nbm).
  • Oracle JDeveloper 11g R2: No integration yet. Still a mystery.
  • HUD: In order to benefit from Unity HUD for Java Swing applications, you need to manually create a myjavaapp.desktop file and put it in ~/.local/share/applications/

e.g:

[Desktop Entry]
Exec=java -jar /home/myusername/mySwingApp/mySwingApplication.jar
Terminal=false
Type=Application
StartupNotify=true
Categories=Java;Science
Icon=/home/myusername/mySwingApp/mySwingApp.png
Name=MySwingApp 1.2

MANUALLY UNINSTALL IF YOU WANT

For Open JDK :
cd ~/jayatana && sudo ant deinstall && sudo ant deconfig
for Oracle JDK:
cd ~/jayatana && sudo ant deinstall && sudo deconfig -Djava.home.7=ORACLE_JAVA_HOME_7
where ORACLE_JAVA_HOME_ is your JDK home directory (most likely /usr/lib/jvm/myjdk7)

About these ads

70 thoughts on “Integration of Java Swing Applications with Unity Global Menu in Ubuntu 12.04

  1. Dan Jared

    Hi,

    You only remember use method AyatanaDesktop.isSupported() to check if environment can use Ayatana Desktop. Download the source, you can see the netbeans plugin source.

    Reply
  2. CuriousGuy

    Is there any information on how to use this library to interact with the Launcher ? (meaning using the progress bar and putting a number on it)

    Reply
    1. Hanynowsky Post author

      You’re indeed a curious guy! For now it’s not yet implemented. The plugin for netbeans handles the Launcher (forcing) through a DEsktop file.
      What needs to be done is synchronizing data from Netbeans download progress bar for example (or any threaded data from a java app) with its entry (Observer) in the launcher. This requires some hacking. (What you mean is the progress bar in Nautilus or USC..)
      I’d report this to Jared. That’s a great bonus.
      Anyway, you could subversion the project on your Neteans and check it Meanwhile.

      Reply
    1. Hanynowsky Post author

      Hi. It should definitely work in your case.
      It’s working fine for me for Netbeans 7.1.2 under Ubuntu 12.04.
      We try to understand what’s wrong together. First of all do you use, OpenJDK or Sun JDK?
      And what did you do exactly to enable it? You install it the NBM plugin? The latest version?

      Reply
      1. lpanebr

        Hi, I found out the problem!

        I also commented about my issue at: http://code.google.com/p/java-swing-ayatana/issues

        It turns out that I had Open JDK 6 and it only work with Open JDK 7.

        I had to install it with: sudo apt-get install openjdk-7-jdk

        And then edit /etc/netbeans.conf to change the netbeans_jdkhome accordingly.

        Now it works very nice! :-)

        Thanks

        PS: I probably did not have version 7 because I upgraded my ubuntu from 11.10.

        Reply
        1. Mabowle

          Thank you for that very important clue. Now it finally works for me, too!

          Maybe a short note, that JDK7 is required should be included in the Plugin description.

          Reply
  3. Pingback: Patch OpenJDK to support Unity Global Menus | Ubuntu Linux FAQs

    1. Hanynowsky Post author

      Hello. Plugin is for now available for Netbeans & JDeveloper.
      IntelliJ IDEA is on the list for future integration. However, a better featuring is installing Java Ayatana as a component tight to OpenJDK7 or Oracle JDK 7. This way, any Java Swing Application benefits from Global Menu integration, and thus all Java based IDEs that implement JMenuBar. This feature is already there but not yet tested. I’ll issue this on the source website and feedback soon.

      Reply
        1. Hanynowsky Post author

          Hello. Try SOLUTION 2 in the post. It Works for the latest Intellij IDEA under Ubuntu 12.10. Not tested yet in 12.04, but it definitely should work too.

          Reply
      1. Udit Mahajan

        Hi I have managed to create the plugin and it works perfectly now. How can I distribute this plugin for others?
        I have the zipped file from “Build” that I have added as plugin.
        Also, I have a weird problem though. My plugin is shown as “Plugin Display name here” in the plugins list. This is very annoying. Though it works like a charm…

        Reply
        1. Hanynowsky Post author

          Hello back. As you can see in solution 2 above in the post, there is no need no more for a plugin, either for Netbeans or Intellij IDea. You just have to install jayatana as an external library for your JDK (Oracle JDK or OpenJDK); then any Java Swing application you run should benefit from Global Menu integration. I have just tested again with Intellij Idea and Netbeans and it’s working fine.
          However, thanks for the plugin. You can submit it though to IntelliJ extension repository and give us back the link when approved.

          Reply
  4. David

    Hi, I tested solution 2 on 12.10 x64 with IntelliJ 12 EAP. The menu does integrate into the global menu but the space in IntelliJ where the menu would have been is still there with some corrupted looking graphics. Some of which appear to be messages or graphics that were at the very bottom of the window.

    I also opened up visual VM while have the test running. VisualVM looked prefect.

    Reply
    1. Hanynowsky Post author

      I don’t think I have this issue. I have IntellijIdea 11.1 for Java. (See second screenshot in the post above).
      12 EAP version’s changes include nothing that seem to be related to menus or integration with Linux desktops, so it basically should work fine just as 11.1. Could you provide a link to a screenshot illustrating your what you described? And Do you have the same issue with IntellijIdea 11.1?

      Reply
      1. David

        Here is the IntelliJ 12 global menu, not maximized http://s15.postimage.org/qdrnwmq2j/Intelli_J_Global_Menu.png

        Here it is maximized

        There is no corruption here but there is a white bar where the menu used to be.

        With IntelliJ 11.1 the behavior is actually similar but you don’t notice it on the start page if maximized because the top of that page is white anyways.
        Once in a while it does seem the menu bar disappears without corruption in 11. But if I maximize and restore a couple times it gets corrupted.

        I am using Oracle JDK 1.7.0_09 – 64bit on Ubuntu 12.10.

        Reply
        1. Hanynowsky Post author

          That’s weird.
          I cannot reproduce this issue in my machine. Yet, I am using release 04 of Oreacle JDK 1.7.
          I doubt it’s related to the JDK or Jayatana.

          Are you sure it’s not a mattre of RAM over-consumption when you run the IDE?
          Could reproduce the issue using AMBIANCE Ubuntu Theme?

          Reply
          1. David

            Did you try changing the IntelliJ theme (settings -> appearance) to something other than GTK+. For instance the Alloy Glass Theme. The jvm has 2GB of heap and the machine has 32GB. I opened a brand new simple project just to make sure it was nothing with my old project. Also with the IntelliJ 11 test I killed the old intellij folder and setup a new instance with 0 plugins enabled. It still had the issue.

            The Ubuntu Theme doesn’t seem to effect anything. I have tested Ambiance and Radiance.

            For me and Udit Mahajan has confirmed the same thing now. It works with the GTK+ theme and has corruption with other IntelliJ themes.

            To summarize my current situation/understanding:
            1) The only variable with it it working or not is the IntelliJ theme. GTK+ = perfect, other themes = corruption.
            2) I have tested the corruption under both Ubuntu’s themes Ambiance and Radiance.
            3) JVM is Oracle JDK 1.7u9, 64bit
            4) OS is Ubuntu 12.10, x64 and is a fresh install.
            5) IntelliJ has 2GB heap and system has 32GB of ram.
            6) nVidia graphics with the latest 304 series driver (304.64).

            I have tested most of the variables I can think to test easily and the IntellIJ theme seems to be the culprit. Hanynowsky, are you seeing correct behavior with an IntelliJ theme?

        2. Hanynowsky

          I experience slightly this issue when switching to Nimbus Theme.
          It looks fine with Metal and the very ugly Motif.
          I think those customized themes like the Alloy or even Nimbus, do not take into consideration the main menu export or maybe that’s just a known bug of IntelliJIdea that does not yet handle theming (look and feel) properly.
          The native default IDEA 4.5 theme which I am using is working fine.
          I’d be investigating later though.

          Reply
          1. David

            On further testing it appears that only the Alloy themes are having issues for me. With IntelliJ 12 I am not seeing any issues with Nimbus. Perhaps some warning on the IntelliJ instructions in the blog to avoid Alloy themes.

            The menus even work with the new Darkula theme, although it has other unrelated issues but it isn’t done yet.

        3. Hanynowsky

          Happy it’s only a matter of external themes.
          reporting a bug over IntellijIdea theme support is also meaningless as Alloy themes are not official.
          Anyway, thanks for your feedbacks and happy coding ;)

          Reply
      2. Udit Mahajan

        Same thing is happening to me too. But not on IntelliJ idea. It is happening on Phpstorm and other jetstorm’s IDEs. On IntelliJ Idea it runs perfectly fine. The same happens with the plugin I earlier created.
        I will post screenshots also in a short while..

        Reply
        1. David

          The IntelliJ issue has to do with which theme you are using. It works with GTK+, other themes seem to have trouble (such as Alloy Bedouin which I typically use).

          Reply
          1. Udit Mahajan

            I am using Default Ambiance theme only.. I even tried changing themes. Its still the same problem.

          2. David

            Sorry about not being precise, I meant the IntelliJ theme, not the Ubuntu theme. It is in the settings -> appearance of IntelliJ.

  5. Udit Mahajan

    Well you are right… If only the GTK+ theme was not ugly now. But i guess everything cannot be perfect. Thank you so much for your help… :)

    Reply
  6. Owen

    Hello, Mr.Hanynowsky. I’m new to java. I followed your instructions step by step and built a really great java-apps-running environment. It works on almost every java app except Eclipse Juno. Does it conflict with Eclipse? Or it’s just me. If any help, my java edition is Oracle Java 8, and I built a RAMDISK for java from this web site:
    Ubuntu & Debian Ramdisk http://prezi.com/xgwyp3reqanc/ubuntu-debian-ramdisk/

    Reply
      1. Owen

        Thank you for the information, Jared. I thought it was me modifying something in Ubuntu so I can’t get eclipse done with it.

        Reply
    1. Hanynowsky Post author

      As said by Jared, Eclipse is not based on Swing but on SWT, and it’s blacklistet in libappmenu.so library for a reason. Anyway, if you want to enable Global menu for Eclipse, check the post:

      Reply
      1. Owen

        Also thank you, Hanynowsky. But I can’t see the post link you’re referring to. Would you mind posting the link again?

        Reply
  7. portoist

    I have came across this guide and got it working with PyCharm on Ubuntu 12.10 with oracle jdk. Thanks a lot! I have used solution 2 and I had some problems running sudo ant install, I have found out that build.xml line 266 has . I have changed it to and it built successfully.

    Reply
  8. Tobias

    Sadly it does not work on kde with the global menu plasmoid. The menus are shown, but unusable, also the submenus don’t work. Is there an option to disable it? The current jabref is using it and I found no option to disable it.

    Reply
  9. Vadim P.

    For Oracle, the instructions still say:

    sudo ant install -Djava.home.7=/usr/lib/jvm/java-7-openjdk-amd64

    I think that is supposed to use be oracle not OpenJDK. The build fails otherwise.

    Reply
        1. Hanynowsky Post author

          Sorry! Seems like lot of stuff has changed in 13.10. This distro is a transitory one towards : 14.04. So I won’t adopt it personally! I am awaiting the 14.04.
          But anyway! I am starting a full analysis for 13.10 very soon! Sorry ( I am busy these days).
          Once Done, I’d post.

          Reply
  10. Rael G.C.

    In the Oracle JDK (Solution2), there is a little mistake.

    sudo ant install -Djava.home.7=/usr/lib/jvm/java-7-openjdk-amd64

    Should be:

    sudo ant install -Djava.home.7=/usr/lib/jvm/oraclejdk7

    Reply
        1. Severin

          I can confirm that Jayatana has no effect on IntelliJ IDEA on Ubuntu 13.10 (using Oracle JDK 7). I wonder if it is hard to make it work again. Anyway, thank you for your effort in providing deeper Ubuntu desktop integration for Java Swing applications!

          Reply
          1. Hanynowsky Post author

            Hi Severin! 13.10 is still BEta and some changes to Libraries may happen again.
            Once 13.10 Final is released, a solution would be posted too.
            I recall Ubuntu Dev Team wanted to integrate Jayatana in Ubuntu OpenJDK package by default; I hope this is going to happen for 13.10.
            Before being sure it does not work for 13.10 (I still myself haven’t tested), use the Jayatana Plugin for one of your Java Swing Test applications and check whether it works or not!
            (I suspect they deeply modified the appmenu.SO library :(

    1. HanynowskyHanynowsky

      Hello,
      Indeed; Jayatana does not support Ubuntu 13.10 yet. Many changes were committed to libappmenu.so and global menu proxy. Ubuntu is transiting towards Unity 8 and MIR and things are changing constantly before the release of 14.04.
      There is a new version of Jayatana being worked for 13.10. Once finished and tested I’d post it here.
      Note that Jayatan only supports Ubuntu 12.04, 12.10 and 13.04.

      Reply
    1. HanynowskyHanynowsky

      Hello,
      Indeed; Jayatana does not support Ubuntu 13.10 yet. Many changes were committed to libappmenu.so and global menu proxy. Ubuntu is transiting towards Unity 8 and MIR and things are changing constantly before the release of 14.04.
      There is a new version of Jayatana being worked for 13.10. Once finished and tested I’d post it here.
      Note that Jayatan only supports Ubuntu 12.04, 12.10 and 13.04.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s