How to notify the user of important events for a d

2020-07-23 05:15发布

问题:

Our customer is using our software (Java Swing application started using Webstart) besides other software like MS Office, to do his job. From time to time there are important events he has to deal with in our software without much delay. The customer wants to have a prominent notification then. Now he might be using Excel at the moment, so popping up a message box will not suffice (although the entry in the task bar will be flashing).

We need some mechanism like the Outlook notifier, i.e. a popup that is always visible but does not steal the focus. The notifier should be displayed all the time until the reason for the message has gone (the user has solved the issue) or the user has closed the message (like in outlook). Additionally we want to display a tray icon and maybe play a sound.

I've tried the Java6 java.awt.SystemTray as well as the JDIC (version 0.9 since we already have that lib in the classpath of that project) equivalent, but I did not find a way to set the timeout of the TrayIcon.displayMessage method, and the message seems not to be always on top.

Is there any other option besides JNI to achieve the requested behavior?

If JNI is the only choice and since Windows is the only platform of our customers, is the Outlook notifier an Outlook only feature, or is it freely usable through the Windows API?

Or what other options exist to notify the user about an important task to perform in one software without hindering him to complete his current task in another software. A system modal dialog therefore is no option!

回答1:

Try using setAlwaysOnTop on your JFrame/JWindow.



回答2:

With OS X the obvious answer would be to use Growl. But there exists a little project doing a similar service on windows environments. It's called Snarl. This might give you a new option to try.

Drawback: You'll have to install a tool on the client's machines. From your description I assume you have a defined group of users on company workplaces, right? So this might acceptable, nevertheless.



回答3:

Using Tray: Which component are you using to show message (JPopup, JDialog, JFrame, JWindow)?

Whichever you use, try to make it unfocusable(override isFocusable method) and call toFront.

Also let me know the result.



回答4:

If you've got a budget, consider a license for JIDE. JIDE has an alert popup designed to look exactly like the outlook popup/alert widget.



回答5:

Here is an example:

class AlertWindow extends Window implements MouseListener
{
    AlertWindow(JFrame frame)
    {
        super(frame);
        this.setAlwaysOnTop(true);
        this.setFocusable(false);
        this.setSize(200, 200);
        this.setLocation(500, 0);
        this.setBackground(Color.BLACK);
        addMouseListener(this);

        try {
            Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
            Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
            mSetWindowOpacity.invoke(null, this, Float.valueOf(0.50f));
        } catch (NoSuchMethodException ex) {
            ex.printStackTrace();
        } catch (SecurityException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    public void mouseClicked(MouseEvent e)
    {
        this.setVisible(false);
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }
}