little bug with SystemTray in Java SE 6 ?

hello there i've made a sample code of SystemTray With Java SE 6

when the user clicks the standard close button on the frame the program is minimized to tray and when he double clicks on the trayicon

the program is restored.

My bug is that if the program is minimized to tray and the user re-opens

the jar file a new copy of the program runs!!!

and i want to make that if the user tries to re-open the jar file (while the program is minimized to tray) the program is restored not opening another copy

how to do that?

here is my sample:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.text.NumberFormatter;

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.Image;

import javax.swing.ImageIcon;

import java.awt.Toolkit;

import java.awt.Container;

import java.awt.event.ActionListener.*;

import java.awt.event.ActionEvent.*;

import java.lang.Runtime;

import java.io.*;

import java.io.IOException;

import java.rmi.*;

import java.rmi.server.*;

import java.math.*;

import java.util.*;

import java.text.*;

import javax.swing.text.*;

import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.AbstractButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.AWTException;

import java.awt.Desktop;

import java.awt.Image;

import java.awt.SystemTray;

import java.awt.Toolkit;

import java.awt.TrayIcon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.util.EventObject;

class systemtrayextends JFrame

{

private Desktop desktop;

private TrayIcon trayIcon=null;

private SystemTray tray;

private PopupMenu popup;

private MenuItem unHideItem;

private MenuItem defaultItem;

public systemtray()

{

super("System Tray");

if (Desktop.isDesktopSupported()){

desktop = Desktop.getDesktop();

}

initComponents();

setSize(350,200);

setResizable(false);

setVisible(true);

}

protectedvoid UnHide()

{

setVisible(true);

setupSystemTray();

}

privatevoid initComponents(){

setupSystemTray();

}

privatevoid setupSystemTray(){

if (SystemTray.isSupported()){

final SystemTray tray = SystemTray.getSystemTray();

Image image = Toolkit.getDefaultToolkit().getImage("D:\\Other\\JAVA\\Icons\\shutdown.png");

ActionListener exitListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

System.exit(0);

}

};

ActionListener unHideListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

UnHide();

tray.remove(trayIcon);

}

};

ActionListener actionListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

UnHide();

tray.remove(trayIcon);

}

};

PopupMenu popup =new PopupMenu();

MenuItem defaultItem =new MenuItem("Exit");

defaultItem.addActionListener(exitListener);

MenuItem unHideItem =new MenuItem("Restore");

unHideItem.addActionListener(unHideListener);

popup.add(unHideItem);

popup.add(defaultItem);

trayIcon =new TrayIcon(image,"JShutdown Timer",popup);

class TrayActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent ee){

trayIcon.getPopupMenu();

}

};

trayIcon.setImageAutoSize(true);

trayIcon.addActionListener(actionListener);

trayIcon.addActionListener(new TrayActionListener());

try{

tray.add(trayIcon);

}catch (AWTException e){

}

}

else{

}

}

publicstaticvoid main(String args[]){new systemtray();}

}

[8281 byte] By [First_knighta] at [2007-11-27 8:29:20]
# 1

I may be wrong, but I do not think this is possible to do in native java.

My hackish sort of an idea would be this.

When your initial program is spawned, write to a file indicating the program is open (firefox does this too).

In your program, you would have to check if this certain file was there:

-If it is, write to another system file indicating that the program needs to be maximized, and exit.

-If it is not, open the program normally.

Your program would need to check for this other system file every so often and then sleep. If it finds that file, bring the window to the front, and delete the file.

Again, this is pretty hackish, and most definitely not ideal, but it's all that I can think of.

Who knows, there may be a better way to do this in Java, it's just a way that I do no know :)

hoozdapimpa at 2007-7-12 20:19:35 > top of Java-index,Java Essentials,New To Java...
# 2
thanks hoozdapimp,but seems this is a long waybut first i'd like to say that i'm not using native to make my system tray!i'm using the class SystemTray in Java SE 6thanks at allany other ideas?
First_knighta at 2007-7-12 20:19:35 > top of Java-index,Java Essentials,New To Java...
# 3
any useful link or tutorial?
First_knighta at 2007-7-12 20:19:35 > top of Java-index,Java Essentials,New To Java...