wikipedia

Support Wikipedia
Showing posts with label tray icon. Show all posts
Showing posts with label tray icon. Show all posts

Thursday, July 9, 2009

Java System Tray icon in Ubuntu

Download JDIC.
  • Using Synaptic package manager
    search for jdic and download both libjdic -java and libjdic-bin packages.
  • downloading from JDIC website
    download the linux distribution from here.
    Say you extract it to the directory /home/myuser/jdic
    You should see a linux folder which has jdic.jar and some .so files.
Here is a code example for creating a tray icon.


// Start Tray Icon Code
SystemTray tray = SystemTray.getDefaultSystemTray();
TrayIcon ti;

try {
if (Integer.parseInt(System.getProperty("java.version").substring(2, 3)) >= 5)
System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
menu = new JPopupMenu("MY APP Menu");

// "Quit" menu item
menuItem = new JMenuItem("Exit");
menuItem.addActionListener(this);
menu.add(menuItem);

ImageIcon i = new ImageIcon(MyClass.class.getResource("/images/appImage.png"));

ti = new TrayIcon(i, "Welcome to My App", menu);

ti.setIconAutoSize(true);
ti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MyApp.this.setVisible(!MyApp.this.isVisible());
if (MyAppFrame.this.isVisible() == false) {
ti.displayMessage(
"Welcome to MyApp",
"To open MyApp Application click the icon. To exit the application right click the icon and click the Quit menu item.",
0);
}
}
});

tray.addTrayIcon(ti);

// Construct the GUI for balloon message.
ActionListener al = new ActionListener() {

public void actionPerformed(ActionEvent e) {
MyApp.this.setVisible(!MyApp.this.isVisible());
if (MyApp.this.isVisible() == false) {
ti.displayMessage(
"MyApp",
"To open the MyApp Application click the icon. To exit the application right click the icon and click the Quit menu item.",
0);
}
}
};
exitMnu.addActionListener(al);

} catch (Exception e) {
logger.error("Unhandled error", e);
}

try {
this.setIconImage(new ImageIcon(MyApp.class
.getResource("/images/myApp.png")).getImage());
} catch (Exception e) {
logger.error("Unhandled error", e);
}
// End Tray Icon Code



For this to work, you need to add jdic jar to the build path and also add share library files location(/usr/lib/jni for the first case and /home/myuser/jdic/linux for the second) to the native library path for the project as shown in the picture below.