Sunday 15 August 2010

How can i dynamically change the tray icon image in JavaFX like dropbox tray icon -



How can i dynamically change the tray icon image in JavaFX like dropbox tray icon -

in dropbox, tray icon dynamically change. when file uploading, sync icon shown. when file synchronized, dropbox show other icon(latest status icon).

https://mockupstogo.mybalsamiq.com/projects/icons/dropbox

i want develop same function using javafx

at first call, trayicon.setimage working (blue color tray icon shown). sec call, not working (gray color tray icon not shown). show empty box.

is javafx bugs?

package application; import java.awt.dimension; import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.ioexception; import java.text.dateformat; import java.text.simpledateformat; import java.util.date; import java.util.timer; import java.util.timertask; import javafx.application.application; import javafx.application.platform; import javafx.geometry.pos; import javafx.scene.node; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.control.treeview; import javafx.scene.layout.stackpane; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.stage.stage; import javafx.stage.stagestyle; public class main extends application { // 1 icon location shared between application tray icon , task bar icon. // utilize multiple icons allow clean display of tray icons on hi-dpi devices. //private static final string iconimageloc = // "http://icons.iconarchive.com/icons/scafer31000/bubble-circle-3/16/gamecenter-icon.png"; //private static final string iconimageloc1 = // "http://icons.iconarchive.com/icons/icons-land/metro-halloween/96/cauldron-icon.png"; // application stage stored can shown , hidden based on scheme tray icon operations. private stage stage; // timer allowing tray icon provide periodic notification event. private timer notificationtimer = new timer(); // format used display current time in tray icon notification. private dateformat timeformat = simpledateformat.gettimeinstance(); private treeview<string> treeview; // sets javafx application. // tray icon setup icon, main stage remains invisible until user // interacts tray icon. @override public void start(final stage stage) { // stores reference stage. this.stage = stage; // instructs javafx scheme not exit implicitly when lastly application window shut. platform.setimplicitexit(false); // sets tray icon (using awt code run on swing thread). javax.swing.swingutilities.invokelater(this::addapptotray); // out stage translucent, give transparent style. stage.initstyle(stagestyle.transparent); // create layout javafx stage. stackpane layout = new stackpane(createcontent()); layout.setstyle( "-fx-background-color: rgba(255, 255, 255, 0.5);" ); layout.setprefsize(300, 200); // dummy app hides when app screen clicked. // real app might have interactive ui , separate icon hides app window. layout.setonmouseclicked(event -> stage.hide()); // scene transparent fill necessary implement translucent app window. scene scene = new scene(layout); scene.setfill(color.transparent); stage.setscene(scene); } /** * dummy app, (javafx scenegraph) content, says "hello, world". * real app, might load fxml or that. * * @return main window application content. */ private node createcontent() { label hello = new label("hello, world"); hello.setstyle("-fx-font-size: 40px; -fx-text-fill: forestgreen;"); label instructions = new label("(click hide)"); instructions.setstyle("-fx-font-size: 12px; -fx-text-fill: orange;"); vbox content = new vbox(10, hello, instructions); content.setalignment(pos.center); homecoming content; } /** * sets scheme tray icon application. */ private void addapptotray() { seek { // ensure awt toolkit initialized. java.awt.toolkit.getdefaulttoolkit(); // app requires scheme tray support, exit if there no support. if (!java.awt.systemtray.issupported()) { system.out.println("no scheme tray support, application exiting."); platform.exit(); } // set scheme tray icon. java.awt.systemtray tray = java.awt.systemtray.getsystemtray(); /* url imageloc = new url( iconimageloc ); url imageloc1 = new url( iconimageloc1 ); java.awt.image image = imageio.read(imageloc); java.awt.image image1 = imageio.read(imageloc1);*/ dimension size = tray.gettrayiconsize(); bufferedimage bi = new bufferedimage(size.width, size.height, bufferedimage.type_int_rgb); graphics g = bi.getgraphics(); g.setcolor(java.awt.color.blue); g.fillrect(0, 0, size.width, size.height); system.out.println(size.width); system.out.println(size.height); java.awt.trayicon trayicon = new java.awt.trayicon(bi); // if user double-clicks on tray icon, show main app stage. trayicon.addactionlistener(event -> platform.runlater(this::showstage)); // if user selects default menu item (which includes app name), // show main app stage. java.awt.menuitem openitem = new java.awt.menuitem("hello, world"); openitem.addactionlistener(event -> platform.runlater(this::showstage)); // convention tray icons seems to set default icon opening // application stage in bold font. java.awt.font defaultfont = java.awt.font.decode(null); java.awt.font boldfont = defaultfont.derivefont(java.awt.font.bold); openitem.setfont(boldfont); // exit application, user must go scheme tray icon // , select exit option, shutdown javafx , remove // tray icon (removing tray icon shut downwards awt). java.awt.menuitem exititem = new java.awt.menuitem("exit"); exititem.addactionlistener(event -> { notificationtimer.cancel(); platform.exit(); tray.remove(trayicon); }); // setup popup menu application. final java.awt.popupmenu popup = new java.awt.popupmenu(); popup.add(openitem); popup.addseparator(); popup.add(exititem); trayicon.setpopupmenu(popup); // create timer periodically displays notification message. notificationtimer.schedule( new timertask() { @override public void run() { javax.swing.swingutilities.invokelater(() -> trayicon.displaymessage( "hello", "the time " + timeformat.format(new date()), java.awt.trayicon.messagetype.info ) ); system.out.println(size.width); system.out.println(size.height); bufferedimage bi = new bufferedimage(size.width, size.height, bufferedimage.type_int_rgb); graphics g = bi.getgraphics(); g.fillrect(0, 0, size.width, size.height); g.setcolor(java.awt.color.gray); trayicon.setimage(bi); } }, 5_000, 60_000 ); // add together application tray icon scheme tray. tray.add(trayicon); //trayicon.setimage(image1); //} grab (java.awt.awtexception | ioexception e) { } grab (java.awt.awtexception e) { system.out.println("unable init scheme tray"); e.printstacktrace(); } } /** * shows application stage , ensures brought ot front end of stages. */ private void showstage() { if (stage != null) { stage.show(); stage.tofront(); } } public static void main(string[] args) throws ioexception, java.awt.awtexception { // launches javafx application. // due way application coded, application remain running // until user selects exit menu alternative tray icon. launch(args); } }

i think problem in different image sizes , alter sec icon sizes of first!

javafx javafx-2 awt javafx-8 trayicon

No comments:

Post a Comment