How to/What is a good library, to create a fading indicator message in Java like that of Outlook when you get a message, or Ubuntu/Gnome when you've connected to a network?
Java 1.6 has a TrayIcon class that can be used to display notification messages.
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
tray.add(trayIcon);
trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
Here's the result:
On Linux you may also have a little program called notify-send. It makes it easy to invokes the standard freedesktop.org notification system from the shell. You can also run it from Java.
String[] notifyCmd = {"notify-send", "Hello, World!"};
Runtime.getRuntime().exec(notifyCmd);
I had to apt-get install libnotify-bin to get this on my Ubuntu box.
I've tested these things on Windows 7 and Ubuntu 9.10. In each case the notification disappeared after some time which is I suppose the fading indicator effect that you want.
I would use Trident animation library. Your task would be almost trivial if you use it.
Also you could take a look at Timing Framework, but it wasn't updated for a long time.
Related
I'd like to customize a notification message in Windows 10 by adding two buttons in order to trigger two different actions. Microsoft toast content tutorial does exactly what I want, but the examples are in C# (which I've never used) and XML which I'm a bit familiar with. Is there a way to achieve the same result in pure java? Or alternatively how can i combine java and xml?
Here is an example of what I want, directly taken from the previously referenced tutorial.
At the moment I have managed to create a simple notification icon but as you can see from the output the are no actions.
public static void notify() throws AWTException{
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage("");
TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Demo");
tray.add(trayIcon);
trayIcon.displayMessage("Hello, World", "notification demo", TrayIcon.MessageType.INFO);
}
AWT's TrayIcon class has a method called displayMessage that shows a native OS message that in Windows 10 looks like this:
as a pop up and like this:
in the notification area.
Can JavaFX do this natively? I know JavaFX doesn't implement traybar support yet and one has to use AWT, but are these notifications traybar dependent?
Apparently javaFx Still doesn't provide way to show tray notifications, but you can use 3rd party library to achieve your goal .
TrayNotification
String title = "Congratulations sir";
String message = "You've successfully created your first Tray Notification";
Notification notification = Notifications.SUCCESS;
TrayNotification tray = new TrayNotification(title, message, notification);
tray.showAndWait();
*
*
*
*
*
*
*
*
ControlsFX
Notifications.create()
.title("Title Text")
.text("Hello World 0!")
.showWarning();
you don't need any 3drparti libraries.
you can use osascript and display in the mac.
// java version
Runtime.getRuntime().exec(new String[] { "osascript", "-e", "display notification \"Message\" with title \"Title\"" });
// koltin version
Runtime.getRuntime().exec(arrayOf("osascript", "-e", "display notification \"Message\" with title \"Title\""))
For anyone coming to this question in 2020, here's a stab at showing native OS notifications working with JavaFX:
https://gist.github.com/wiverson/d2edf0d66ad195c96793d0d25290753b
As noted in the sample, OS native notifications are best if the app is in the background - use ControlsFX notifications if the app is in the foreground.
This works on macOS Big Sur, should also work on Windows.
[Edit 1/5/21] Here is a project that also will help with this:
https://github.com/dustinkredmond/FXTrayIcon
I'm creating a Java program which pushes notifications and I want to remove or edit the line which says "Java(TM) Platform SE binary".
Is this possible? I searched on google but I couldn't see info about this.
Here is the related code. The last line is the line that pushes the notification.
public void mostrarNotificacion(Usuario user) throws AWTException, java.net.MalformedURLException, IOException {
//Obtain only one instance of the SystemTray object
SystemTray tray = SystemTray.getSystemTray();
//Get image
BufferedImage trayIconImage = ImageIO.read(getClass().getResource("favicon.png"));
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
//Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);
//Set tooltip text and notification text
if(user.getDescargos()>=5 || user.getOtrosPendientes()>=1){
mensaje = "Descargos pendientes: " + user.getDescargos() + "\nSecciones pendientes: "+ user.getOtrosPendientes();
}
trayIcon.setToolTip(mensaje);
tray.add(trayIcon);
trayIcon.displayMessage("Pendientes EKHI", mensaje, MessageType.WARNING);
}
"Java(TM) Platform SE binary" doesn't appear if you use TrayIcon.MessageType.NONE
Example Image:
The only way to solve this is to use a java native launcher.
My best choice would be javapackager which allows you to bundle a local copy of the JVM to make your app completely portable.
Other option is launch4j which just creates a launcher but still requires Java installed.
Code:
public byte[] getThumbnail(byte[] imageBytes) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).size(50, 50).keepAspectRatio(true)
.outputFormat("jpg").toOutputStream(outputStream);
byte[] picture = outputStream.toByteArray();
return picture;
}
I am trying to generate a thumbnail from an image in the above code.
When I call the above function, it launches a Java icon, which is shown in my attached screenshot. If I try to close this icon, my application gets closed.
The dock icon appears, because some of the imaging code you use, use awt under the hoods. This triggers the dock icon to appear on OS X. It's possible to suppress the icon, though.
The cross platform way of doing it, is running you application in "headless" mode, that is, with no user interaction using mouse, keyboard or screen feedback (ie. windows). You can specify headless mode at startup, using the system property java.awt.headless on the command line like this:
java -Djava.awt.headless=true
Alternatively, in code like this:
System.setProperty("java.awt.headless", "true");
For OS X (and an Apple JRE) you can alternatively use the system property apple.awt.UIElement, it will suppress the dock icon only, but otherwise let your app use windows etc.:
java -Dapple.awt.UIElement=true
From the documentation:
Suppresses the normal application Dock icon and menu bar from appearing. Only appropriate for background applications which show a tray icon or other alternate user interface for accessing the apps windows. Unlike java.awt.headless=true, this does not suppress windows and dialogs from actually appearing on screen.
The default value is false.
I would like to show some numbers on my tray icon indicating a number of events that happened to the user like what is done in this facebook notifications icons:
Do you think that it is possible ?
Thank you
You can do this using the TaskBar and TaskItem classes although it may not work on all platforms.
TaskBar taskBar = Display.getDefault().getSystemTaskBar();
// TODO may return null if not supported on the platform
// Get application item
TaskItem taskItem = taskBar.getItem(null);
if (taskItem != null)
taskItem.setOverlayText("your text");
Also try:
TaskItem taskItem = taskBar.getItem(shell);
where shell is your main application shell. The TaskItem JavaDoc suggests trying both methods of getting the TaskItem:
For better cross platform support, the application code should first
try to set this feature on the TaskItem for the main shell then on the
TaskItem for the application.