adding and removing trayicon in java? -
i want add icon system tray when window minimized , remove when maximized exception , can't solve it.
exception in thread "awt-eventqueue-0" java.lang.illegalargumentexception: adding trayicon added.
else if (e.getsource() == minimizebutton) setstate(islamicproject.iconified); { // test see if supports tray if (systemtray.issupported()) { //create tray tray = systemtray.getsystemtray(); image image = toolkit.getdefaulttoolkit().getimage("d:/art gallary 2008/islamic/forsan_03.gif"); //create menu items popupmenu popup = new popupmenu(); menuitem exitmenu = new menuitem("exit"); menuitem openmenu = new menuitem("open"); trayicon = new trayicon(image, "the tip text", popup); //add listeners of menu items listenforexitmenu exmu = new listenforexitmenu(); listenforopenmenu opmu = new listenforopenmenu(); exitmenu.addactionlistener(exmu); openmenu.addactionlistener(opmu); //adds listener when icon in tray clicked opens //trayicon.addactionlistener(opmu); mouselis l = new mouselis(); trayicon.addmouselistener(l); // window state listener tests see state of frame addwindowstatelistener(new windowstatelistener() { public void windowstatechanged(windowevent e) { if (e.getnewstate() == iconified) { try { tray.add(trayicon); setvisible(false); } catch (awtexception ex) { system.err.println("can't add tray"); } } if (e.getnewstate() == normal) { tray.remove(trayicon); setvisible(true); } } }); // adding open , exit menu popup.add(openmenu); popup.add(exitmenu); } else { system.err.println("tray unavailable"); } }
here example:
import java.awt.*; import java.awt.event.*; import javax.swing.jframe; import javax.swing.uimanager; /** * * @author mohammad faisal * ermohammadfaisal.blogspot.com * facebook.com/m.faisal6621 * */ public class hidetosystemtray extends jframe{ trayicon trayicon; systemtray tray; hidetosystemtray(){ super("systemtray test"); system.out.println("creating instance"); try{ system.out.println("setting , feel"); uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); }catch(exception e){ system.out.println("unable set lookandfeel"); } if(systemtray.issupported()){ system.out.println("system tray supported"); tray=systemtray.getsystemtray(); image image=toolkit.getdefaulttoolkit().getimage("/media/faisal/dukeimg/duke256.png"); actionlistener exitlistener=new actionlistener() { public void actionperformed(actionevent e) { system.out.println("exiting...."); system.exit(0); } }; popupmenu popup=new popupmenu(); menuitem defaultitem=new menuitem("exit"); defaultitem.addactionlistener(exitlistener); popup.add(defaultitem); defaultitem=new menuitem("open"); defaultitem.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { setvisible(true); setextendedstate(jframe.normal); } }); popup.add(defaultitem); trayicon=new trayicon(image, "systemtray demo", popup); trayicon.setimageautosize(true); }else{ system.out.println("system tray not supported"); } addwindowstatelistener(new windowstatelistener() { public void windowstatechanged(windowevent e) { if(e.getnewstate()==iconified){ try { tray.add(trayicon); setvisible(false); system.out.println("added systemtray"); } catch (awtexception ex) { system.out.println("unable add tray"); } } if(e.getnewstate()==7){ try{ tray.add(trayicon); setvisible(false); system.out.println("added systemtray"); }catch(awtexception ex){ system.out.println("unable add system tray"); } } if(e.getnewstate()==maximized_both){ tray.remove(trayicon); setvisible(true); system.out.println("tray icon removed"); } if(e.getnewstate()==normal){ tray.remove(trayicon); setvisible(true); system.out.println("tray icon removed"); } } }); seticonimage(toolkit.getdefaulttoolkit().getimage("duke256.png")); setvisible(true); setsize(300, 200); setdefaultcloseoperation(jframe.exit_on_close); } public static void main(string[] args){ new hidetosystemtray(); } }
Comments
Post a Comment