Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a notification in swing

Tags:

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

this , this
(source: maketecheasier.com)
, or this
(source: microsoft.com)

(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 Sliding Notification bar in java (a la Firefox)

like image 417
Jeremy Avatar asked Jul 13 '10 18:07

Jeremy


People also ask

How do you send notifications in Java?

To send a notification, you need to construct an instance of the class javax. management. Notification or a subclass (such as AttributeChangedNotification ), and pass the instance to NotificationBroadcasterSupport. sendNotification .

Can we use HTML in Java Swing?

HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

What is notification in Java?

The notification type. It's a string expressed in a dot notation similar to Java properties. It is recommended that the notification type should follow the reverse-domain-name convention used by Java package names. An example of a notification type is com.


1 Answers

You might need a translucent frame without decorations.

Quick demo

OSX

enter image description here]

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">Oscar Reyes</a>  */ class Translucent 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 Translucent() {         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();         f.setUndecorated(true);         setTranslucency(f);         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         f.setBackground(new Color(0f, 0f, 0f, 1f / 3f));         JPanel p =  new Translucent();         JLabel l = new JLabel("Hola");         l.setFont(new Font(l.getFont().getName(), Font.PLAIN, 128));         p.add(l);         f.add(p);         f.pack();         f.setLocationRelativeTo(null);         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);                if (!mSetWindowOpacity.isAccessible()) {                    mSetWindowOpacity.setAccessible(true);                }                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();             }     } } 
like image 118
OscarRyz Avatar answered Oct 21 '22 09:10

OscarRyz