System Tray (Menu Extras) icon in Mac Os using Java - 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.

Related

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.

Focusing on an external java window? WINDOWS

I'm using a robot to click on a certain point on the screen. I just need a way for the robot to bring the desired window to focus. Now here's where the real problem lies, the window is a Java window, but not one I made. I used a VBscript to focus on Firefox earlier, but I'm not sure what to do. When I mouse over the icon on my toolbar, it says the windows name is "BitMinter Client v1.4.2" but in Task Manager it calls the process "Java(TM) Platform SE binary (32 bit)?
How do I focus in on the window?
Having the robot input ALT+TAB won't work because the window isn't second on my ALT+TAB list.
AppActivate looks for matching Window's titles.
Use spy++ (Windows SDK and most MS development tools) to get the window title.
You mention firefox so I don't know what type of Window you are referring to. In IE most web page elements (and for that matter very many other Window's things) haven't been Windows for decades. I don't know how firefox or java are implemented.
Windows basic architecture is windows (such as edit controls/buttons) inside other windows (a pane) inside a top level window (ie notepad) inside the desktop window.
To take notepad. There is a main window, menu bar, statusbar window, edit control window. Each of them has a title and class. This is notepad's title and class.
Untitled - Notepad Notepad
«No Window Text 0» Edit
«No Window Text 0» msctls_statusbar32

Java command line application and Systray

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.

Create an inspector style window using Swing/AWT

Is there a way to create a window using Swing or AWT that behaves and looks like an inspector window on Mac OS X? An example of an inspector window would be the window that opens in Finder when Command-Option-I is pressed.
I'm looking for a way to create a window that has a half-height title bar, that always stays on top and that does not get focus e.g. when dragged around.
It is only necessary for the solution to work on Mac OS X, so platform-specific libraries are allowed. But if there is a standard way, event if it has minor drawbacks, it is preferred.
Leopard added some Swing client properties to improve the UI of OS X Java apps – these are described in Technical Note TN2196. The one you're looking for is Window.style:
This property determines if the window has a Utility-style title bar. In order to make this window style also float above all others you must additionally call setAlwaysOnTop(true). Windows that have both the "small" style and are set to always be on top will automatically hide themselves when your application is no longer frontmost. This is similar to how native applications behave.
This property has to be set on the JRootPane of a window before it's native peer is created:
dialog.getRootPane().putClientProperty("Window.style", "small");
ModalityTypes are platform dependent, you have to look for JDialog#ModalityTypes

SWT on OS X: how to handle reopen events

Usually, on OS X when a user clicks the dock icon of a running application, it will show a window - either brings an existing one to front, de-iconifies an existing one or creates a new one.
How to provide a call-back which is invoked when clicking the dock icon of an SWT application?
Have a look at SWT: how to handle application events correctly on OS X
Also, if you just want to see when a window (or Shell in SWT) is shown, then attach an SWT.Activation listener...

Categories

Resources