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

