swing - How to use SystemTray correctly in Java? -
i tried implement small (test) programm using systemtray
in java. got problems , wrote (m)we , pubushed http://pastebin.com/nybkalsy (sorry not doing safty checks).
what happens (under ubuntu/kde openjdk):
- the program starts , button clickable.
systemtray
registered (all right far) - if click (single left mouse) on image in tray button no more clickable. instead seems disabled. right-click on tray icon or frame content (!!!) opens popup. after closing (clicking anywhere) button active again. (this bahavior cannot explain. code not implie far see)
- if click on minimize button of window manager frame disappears (correct)
- on choosing
show
popup of tray icon restores frame , frame content clickable (ok far). - unfortunately popup disabled after that. cannot open again , if minimize window again cannot bring window again. (also not see point in code)
now unsure if understood principles wrongly or if there other bug around. please me clear questions.
edit: inserted code (a bit modifie) here.
please note created image dummy code. in implementation load external image (see comments) produces same result. icontoimage()
hint in comments seems not problem.
import java.awt.awtexception; import java.awt.image; import java.awt.menuitem; import java.awt.popupmenu; import java.awt.systemtray; import java.awt.trayicon; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowevent; import java.awt.event.windowstatelistener; import java.awt.image.bufferedimage; import java.net.url; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; public class testtray { public static void main(string[] args) { // create tmp image image image = new bufferedimage(32, 32, bufferedimage.type_int_rgb); image.getgraphics().drawoval(2, 2, 30, 30); // alternative: load png image //url imurl = testtray.class.getresource("clock.png"); //imageicon icon = new imageicon(imurl); //image = icon.getimage(); trayicon ti = new trayicon(image); ti.setimageautosize(true); try { systemtray.getsystemtray().add(ti); } catch (awtexception e) { e.printstacktrace(); } // create jframe , set default things final jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new jbutton("test-button")); frame.pack(); frame.setvisible(true); // add listener hide window in case of minimization frame.addwindowstatelistener(new windowstatelistener() { @override public void windowstatechanged(windowevent ev) { if(ev.getnewstate() == jframe.iconified) frame.setvisible(false); } }); // create popup system tray , register popupmenu popup = new popupmenu(); menuitem menuitem; menuitem = new menuitem("show"); menuitem.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { frame.setvisible(true); frame.setextendedstate(jframe.normal); } }); popup.add(menuitem); menuitem = new menuitem("exit"); menuitem.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { system.exit(0); } }); popup.add(menuitem); ti.setpopupmenu(popup); } }
Comments
Post a Comment