views:

75

answers:

3

Using Java and Swing, is there any (convenient) way to create a notification? By notification, I mean something like:

this, this, or this

(Is there a more correct term for that?). It would be nice if it worked cross-platform, but I'm mainly concerned with it working under Ubuntu with Gnome. If at all possible, I would like to avoid having an icon in the system tray / notification area.

If all else fails, I could always use the sliding notification from http://stackoverflow.com/questions/290210/sliding-notification-bar-in-java-a-la-firefox

+3  A: 

A standard way to do it is to use Swing TrayIcon API. That would probably be the most convenient way too :)

eugener
At a guess, displayMessage will generate the style shown in the third screenshot. On Windows, at any rate.
R. Bemrose
On my system, that ends up looking like this: http://i.imgur.com/tfTkv.png (I was hoping for something closer to the native gnome notification)
Jeremy
It is possible to create custom notification window, but it would be less "convenient". Basically it involves creating a window with FocusListener such that window closes when it loses focus. Obviously you will be able to do your own panting. For animation effects I would suggest Trident library found at http://kenai.com/projects/trident/pages/Home
eugener
+1  A: 

I haven't used this, but JToaster looks like it might be a good match. Also, are you open to using SWT? This might give you some additional options (here's an example).

Matt Solnit
Good point. I probably should just stop using Swing.
Jeremy
@Jeremy, you probably just need to work more with swing, it is very powerful ( when you get to know it, which btw, may take a while )
OscarRyz
+5  A: 

You just need a translucent frame without decorations.

This is what I did in a couple of minutes, I guess a pretty decent job could be done in a week or so.

On linux

on linux

On windows

on windows

And OSX

on OSX

You can take advantage JLabel displays simple HTML

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.Date;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;


/**
 * Simple demo on how a translucent window
 * looks like when is used to display the system clock.
 * @author <a href="http://stackoverflow.com/users/20654/oscarryz"&gt;Oscar Reyes</a>
 */
public class TranslucencyDemo extends JPanel implements ActionListener {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private final Date now = new Date();
    private final Timer timer = new Timer(1000, this);
    private final JLabel text = new JLabel();

    public TranslucencyDemo() {
        super(true);
        timer.start();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        now.setTime(System.currentTimeMillis());
        text.setText(String.format("<html><body><font size='50'>%s</font></body></html>",sdf.format(now)));
    }

    public static void main(String[] args) {

        JFrame f = new JFrame();
        setTranslucency( f );
        f.setUndecorated( true );
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(new Color(0f, 0f, 0f, 1f / 3f));
        f.add(new Translucent());
        f.pack();
        f.setVisible(true);
    }
    // taken from: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
    private static void setTranslucency( Window window){
        try {
               Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
               Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
               mSetWindowOpacity.invoke(null, window, Float.valueOf(0.75f));
            } 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();
            }
    }
}
OscarRyz
That's the nicest looking thing I've seen so far, so I'll go with it (-: It's too bad that there isn't (as far as I know) a way to use the native notifications from swing.
Jeremy