Java command line application and Systray - java

I am writing Java application, which is totally GUI-less. It runs in terminal through command line and everything is fine. But now I need to add system tray's icon to it in order to provide some notifications to the user. I tried to use java.awt.SystemTray and java.awt.TrayIcon for that. Although icon almost works (leaving look and feel problem aside), my Mac OS puts new application window to the Dock, as if whole Swing application was run.
So, the question: can my GUI-less java application remain totally invisible but for the tray icon? In Mac OS, Windows and Linux.
Edit: I have tried
System.setProperty("apple.awt.UIElement", "true");
This helped me getting rid of Dock icon, but now
trayIcon.displayMessage("Run!", null, TrayIcon.MessageType.ERROR);
does not display message window.

If it doesn't work with Swing maybe you could try
SWT: Tray Icons and Tooltips.

Related

jedit system tray not displayed / jedit window does not appear

I've been using jedit for a few years. I installed it (not recently) with server mode so that jedit-server starts each time my OS starts.
I'm using windows 7 and java 1.8.0_111 and I recently noticed the random lack of jedit system tray icon. By random I mean from time to time (maybe one out of ten times, no obvious periodicity), after windows starts, the system tray icon is not visible.
I usually click on the system tray icon to open a new jedit window. As the system tray icon was missing I tried to launch it manually: programs > jedit > jedit and no window was displayed.
I checked processes and found that the jvm is actually launched but no window is displayed, very odd.
Is there someone with the same issue, a fix or a workaround?
FYI, I did not change jedit or java version recently.
If jEdit does not come up you maybe have a stale server file in your settings directory that points to a port that some process listens on but does not respond how jEdit expects. Unfortunatley up-to-now this situation is not handled too nice by jEdit, as it can end in waiting for an answer forever. So if you find jEdit not coming up, have a look at activity.log in the settings directory to see whether some error is logged, and try deleting the server file from the settings directory after killing the jEdit process and before restarting it.
Actually, I found a workaround, if you start jedit manually from command line using the "noserver" option, the jedit window is displayed as expected:
C:\Program Files\jEdit>java -jar jedit.jar -noserver
It probably means there is something wrong with jedit-server start process or at least with the communication between jedit client and server. I did not found what yet, but at least it gives a workaround!

How to correctly implement SystemTray/Pinned Taskbar applications on Windows with Java

On OSX my Java application has a dock menu (using Apples extension to Java com.apple.eawt.Application.getApplication().setDockMenu) , allowing tasks to be started by right clicking on the dock icon, also files can be dropped onto the dock icon (Using Apples com.apple.eawt.OpenFilesHandler) and my application starts processing the dropped files.
I'm trying to replicate this functionality if sensible on Windows, if this behaviour on Windows is weird I don't want to do it. I cannot find a way to add tasks to to the popup menu for the application icon on the taskbar (my application is has an .exe wrapper provided by winrun4j), is that possible ?
But I have used java.awt.SystemTray to add a right click menu to that, and it works but I'm unclear in Windows when one would use the taskbar icon and when the SystemTray. What I cannot do is have the SystemTray respond to files being dropped onto it, and according to this Oracle Java issue it will never happen http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7119272. What I'm unclear about is if the Windows System tray is never meant to respond to things being dropped on it, or if this is just missing functionality in the Java implementation.
And is there way to drag files onto the taskbar icon instead, or is this whole notion of dragging files onto minimized icons purely an OSX thing and not relevent to Windows ?
To answer my own question files cannot be dropped directly onto pinned taskbar icon or the toolbar icon, but if you drag files onto the taskbar icon it should cause the main window to be displayed and then the files can be dropped onto the window instead, and this behaviour happens automtically with no coding required on my part.

SWT Mac OSX - Unread Message Count (like Mac Mail or Skype)

I've written an SWT app, that on Windows just sits in the System Tray and changes its icon with a message count. This is fine for Windows, but when it comes to the Mac i'd like to make it look a little more native.
I can get an icon to pop itself into the doc area, which is fine, but not i'd like to get an unread message count like those found on Skype or Mac Mail. Is there a way of doing this using SWT?
Cheers
See Snippet336.java, from the SWT snippets page. TaskBar and TaskItem will do what you want. TaskItem#setOverlayText(String) will put a badge on your application's icon.

How can I easily make a java application invisible to the user?

I have developed a Java aplication that is currently being run by double-clicking on a ".bat" file that does something like "java -jar proy.jar". This application just listens on a port and writes to a database, so it does not have any user interface (such as a window). I need this application to run as in background mode, or as it were a service, but I don't really anything more than that. It's enough if the application is run in a way that is not noticeable by the user, so that the user is not bothered and so the application can not be mistakenly closed. By the way, this will be run on an specific computer so it's okay if I have to do any manual configuration ir order to make this work. Also, I need this application to run on startup.
Any help/tips regarding this?
In advance, thank you very much for your help!
Regards,
Pedro
use javaw.exe instead of java.exe and you will not get a console window.
from the java.sun.com:
The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.
EDIT:
A .bat will start a console. If you can live with a console up for a fraction of a second, you can preface your call to javaw with start. This will spawn another console-less process for javaw and allow the console-ful bat to exit.
You can run your application as a System Tray icon (Java 1.6) if you are targeting Windows environment. Allow a context-sensitive menu trigger when clicking on the System Tray icon for the user to select a few options.

System Tray (Menu Extras) icon in Mac Os using Java

I'm developing a desktop application using Java. I want to put an icon (with a contextual menu) on the system tray (called Menu Extras in Mac Os). Java 6 comes with support for doing this in Windows and Linux, but it doesn't work in Mac Os.
I have seen some applications doing what I want in all three operating systems (e.g. DropBox), but I don't know if they are made with Java.
How can I achieve this?
If it's not possible in Java, is there any other cross-platform language able to do that?
Thanks.
AWT / Swing
According to documentation, OSX 10.5 update 1 and newer support TrayIcons
TrayIcons are represented on Mac OS X
using NSStatusMenus that are presented
to the left of the standard system
menu extras. The java.awt.Image
artwork for a TrayIcon is presented in
grayscale as per the Mac OS X standard
for menu extras.
TrayIcon.displayMessage() presents a
small non-modal dialog positioned
under the TrayIcon. The ActionListener
for the TrayIcon is only fired if the
"OK" button on the non-modal dialog is
pressed, and not if the window is
closed using the window close button.
Multiple calls to
TrayIcon.displayMessage() will dismiss
prior messages and leave only the last
message. If the application is not in
the foreground when
TrayIcon.displayMessage() is called,
the application bounces its icon in
the Dock. Message windows are badged
with the application's icon to
identify the which application
triggered the notification.
noah provided this sample:
java.awt.SystemTray.getSystemTray().add(new java.awt.TrayIcon(java.awt.Toolkit.getDefaultToolkit().getImage("foo.png")));
Note that you'll probably want to attach a menu to that icon before adding it to the tray, though.
SWT
According to documentation, SWT 3.3 and newer supports TrayItem icons on OSX.
Icons placed on the system tray will now appear when running on OS X in the status bar.
This snippet shows how to create a menu and icon and put them in the Tray.
I ported a Windows application to my Mac with little difficulty. One thing I noticed is that the icons are in full, living color (not following the Mac convention). I'll need to add a little OS-specific code to convert myself. But this is a big step up from the DLL dependent Desktop integration version from earlier iterations of Java.

Categories

Resources