Grabduck

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);}
}
}

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

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

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)