English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i need to know what to do to run my apllication in the task bar when i close it , you know like the Yahoo messenger or MSN messenger when you close it it runs in the task bar .

2007-08-15 00:51:25 · 3 answers · asked by kmhosny89 2 in Computers & Internet Programming & Design

3 answers

Java is an OS-independent framework (or at least tries to be so), so system-specific facilities such as the Windows System Tray are not supported.

You can do it with native code and JNI (or you can borrow some else's library which does that -- as a respondent below suggested). But it's not supported out of the box, and any support for it is ultimately implemented outside the Java VM, in effect.

2007-08-15 01:43:35 · answer #1 · answered by McFate 7 · 0 2

there is no built-in way in Java to put an icon in a system taskbar, and provide a menu for said application. This is one of those 'hurdles' that many client-side Java developers often face.

Enter JDIC . JDIC stands for J ava D esktop I ntegration C omponents, and is a http://www.Java.net project initiated by Sun Microsystems that is meant to help bridge the gap for building client-side Java applications.

The nice thing about JDIC tray support is that it uses existing Swing code ( javax.swing.JPopupMenu and the corresponding menu classes and event listeners, in addition to javax.swing.Icon to be precise) for all of the API that the developer works with. Because of that, writing a tip on using the JDIC libraries is a walk in the park. In addition, because the menu is rendered using Swing, all of the benefits of the Swing PLAF system are automatically available ( see in my example below).

(If you don't know how to use the existing Swing menu system, see the Java tutorial. and read about javax.swing.JMenuBar and the popup counterpart javax.swing.JPopupMenu .)

The key to building your own tray icon (and menu) is to have three things:


1. The JDIC binary distribution (i.e. JAR and DLL)

2. An Icon

3. The Code to wire up the menu

You can download the JDIC binary distributions from here: http://jdic.dev.java.net - JDIC includes native code, and as such you have to know how setup the Native path - (if you aren't familiar with this process see my post on SWT Projects in Eclipse which discusses in detail how to set up the library path property).

You can get icons from anywhere - I highly recommend making the icon 16x16, as that seems to be a fairly common size that can be comfortably used across platforms.

Finally, the code is something you have to come up with yourself, but I can give you a complete working example - and here it is!

import java.awt.event.*;
import javax.swing.*;
import org.jdesktop.jdic.tray.*;

public class TrayTest {

public static void main(String[] args) {

Icon icon = new ImageIcon(TrayTest.class.getResource("alert_obj.gif"));

JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("Test 1"));

menu.addSeparator();

JMenu subMenu = new JMenu("Test 2");
subMenu.add(new JMenuItem("Test 3"));

menu.add(subMenu);

menu.addSeparator();

JMenuItem exit = new JMenuItem("Exit");

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

menu.add(exit);

TrayIcon tray = new TrayIcon(icon, "My Caption", menu);

SystemTray.getDefaultSystemTray().addTrayIcon(tray);

}
}

2007-08-15 05:47:35 · answer #2 · answered by angel04 3 · 1 0

Say it sys-tray instead of taskbar

here it goes....
A Java programmer often misses the ease with which a VB or Visual C++ application can integrate with the Windows desktop. This is because Java, at its core, being platform independent tries to provide the most common UI components that are expected to be available across various OSs.

Steps to system tray integration

We assume that you are well versed with developing GUI applications with Java Swing. To install a Java application on the system tray, all you need to do is:

* Construct a JPopupMenu (Java Swing)
* Construct one or more JMenuItems and add them to the JPopupMenu (Java Swing)
* Create an ImageIcon (Java Swing)
* Construct a TrayIcon with the JPopupMenu as a pa rameter (JDIC specific)
* Get a reference (a SystemTray object) to the Windows System Tray (JDIC specific)
* Add the TrayIcon to the SystemTray (JDIC specific)

The code for the process is as follows.

// step 1
JPopupMenu menu = new JPopupMenu("Menu");

// step 2
JMenuItem menuItem1 = new JMenuItem("Menu 1");
menu.add(menuItem1);

// repeat step 2 to add more menu items to the JPopupMenu

//step 3
ImageIcon icon = new ImageIcon("icon.jpg");

// step 4
TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu);

// step 5
SystemTray tray = SystemTray.getDefaultSystemTray( );

// step 6
tray.addTrayIcon(trayIcon);

Note that the parameters to the TrayIcon( ) constructor are the ImageIcon object, tooltip (that will show when you move the mouse over the tray icon) and the JPopupMenu respectively.

Following is the code for a ready to compile and run Java program, which will install on the system tray.

import javax.swing.*;
import org.jdesktop.jdic.tray.*;
import java.awt.event.*;

public class SystemTrayDemo extends JFrame{

public SystemTrayDemo(){

JPopupMenu menu = new JPopupMenu("Menu");
JMenuItem menuItem1 = new JMenuItem("Menu1");
menu.add(menuItem1);

JMenuItem menuItem2 = new JMenuItem("Menu2");
menu.add(menuItem2);

JMenuItem menuItem3 = new JMenuItem("Menu3");
menu.add(menuItem3);

JMenuItem menuItem4 = new JMenuItem("Exit");
menu.add(menuItem4);

menuItem4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}});

ImageIcon icon = new ImageIcon("icon.jpg");
TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu);

SystemTray tray = SystemTray.getDefaultSystemTray();
tray.addTrayIcon(trayIcon);
}
public static void main(String[] args){
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing. plaf.windows.WindowsLookAndFeel");
}
catch(Exception e) {
System.out.println(e);
}

new SystemTrayDemo();
}}

Save the code as SystemTrayDemo.java in a directory, systray (say). Create/download any JPEG file in this directory (to display the tray icon) and call it icon.jpg.

Compile and run
Download the J2SE SDK 5.0 Update 1 for Windows from www.java.sun.com. Go to https://jdic.dev.java.net/servlets/ProjectDocumentList and download the JDIC package. Click on jdic-0.8.8 and download the file named jdic-0.8.8-bin-windows.zip. Unzip the archive and copy files named jdic.dll, tray.dll and jdic.jar to the systray directory. Compile the code and execute it as:

javac -classpath jdic.jar;. SystemTrayDemo.java
java -cp jdic.jar;. SystemTrayDemo

You should be able to see a tray icon on the system tray. Right click on it to see the popup menu with menu items-Menu1, Menu2, Menu3 and Exit.

To close or exit, click on Exit to call the System.exit( ) method-specified in the ActionListener for menuItem4 object.


Hope this will help
Cheers:)

2007-08-15 01:50:06 · answer #3 · answered by Neeraj Yadav♥ 6 · 0 0

fedest.com, questions and answers